From 7985115bd1301db867935b52a689ccfc32f13794 Mon Sep 17 00:00:00 2001 From: Florian Zschocke Date: Sat, 10 Dec 2016 16:02:21 +0100 Subject: Fix user mention regular expression and group replacement. The regular expression used for user mentions used to work only inside sentences. Also, since it tested for whitespace, the whitespace would get replaced, too, which would join lines together. Instead the new regex uses boundary matchers to match against word boundaires. As these are not capturing only the actual user mention can be captured and is then replaced. Also, this way the regex can ignore punctuation like in "@jim, look at this." Since Gibtlit now requires Java 7 we can use named capture groups. This makes the use of a centrally defined regular expression much safer. The (admittedly only) group to capture the user name is named "user" and can be referenced by this name. By using the name instead of a group number, the regex could be changed without the code using it breaking because the group number changed. A simple test is added for user mentions, which unfortunately has to deal with the full markdown replacement, too. Fixes #985 --- src/main/java/com/gitblit/models/TicketModel.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main/java/com/gitblit/models/TicketModel.java') diff --git a/src/main/java/com/gitblit/models/TicketModel.java b/src/main/java/com/gitblit/models/TicketModel.java index 924400f5..65e29dc0 100644 --- a/src/main/java/com/gitblit/models/TicketModel.java +++ b/src/main/java/com/gitblit/models/TicketModel.java @@ -778,7 +778,7 @@ public class TicketModel implements Serializable, Comparable { Pattern mentions = Pattern.compile(Constants.REGEX_TICKET_MENTION); Matcher m = mentions.matcher(text); while (m.find()) { - String username = m.group(1); + String username = m.group("user"); plusList(Field.mentions, username); } } catch (Exception e) { -- cgit v1.2.3