问题
I'm using the following regex expression to detect hashtags and mentions in my app.
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(#|@)(\\w+)" options:NSRegularExpressionCaseInsensitive error:&error];
However users within my app are allowed to use some special characters in their usernames. For example @user.name or @user_name. Spaces are not allowed. However using thins regular expression would only detect @user when it should in fact be @user.name. Hostages work perfectly but the special characters in usernames break the mention functionality.
I'm really new to regex and I'm not sure what I need to change to fix this. I'm pretty sure its something to do \\w+ but what exactly I could do with some help.
回答1:
Since you need to match any non-whitespace characters after @ or # but the last character of this sequence must be a word character, you can safely use
@"[#@]\\S+\\b"
Note that an alternative group (#|@) works more effeciently when transformed into a character class [#@] (it involves less backtracking).
Regex breakdown:
[#@]- match a#or@, 1 time\S+\b- match 1 or more non-whitespace characters but the last one must be at the word boundary.
A bit more enhanced version (to make sure the first character after #/@ is a word character and the whole username is at least 1 character long):
@"[#@]\\w\\S*\\b"
Note that this second version will not support such names as @-nick.name-.
来源:https://stackoverflow.com/questions/33735272/nsregularexpression-for-hashtags-and-mentions-with-special-characters