Mark
Mark

Reputation: 3859

Perfect a regex for finding @username tags

Im using a system to get @twitter like names and the following regex is near perfect:

(?<![^\s<>])@([^\s<>]+)

The problem I have found is if there are punctuation marks after the name

So for example:

Obviously we only want to match the username and not the punctuation marks. The caveat is that some usernames have these period inside the username, for example

For example, these are all legitimate usernames

mark.markus

[email protected]

[email protected]

EDIT We are using a lookbehind, if the above usernames are used with an @ infront of them, they should match, but without the @ in front then an email address should actually not match. @[email protected] should match [email protected], but if someone typed plain old [email protected] we dont want gmail.com to match.

Any ideas on how to modify the regex to account for the various punctuation marks that could be used?

Upvotes: 7

Views: 799

Answers (1)

user1919238
user1919238

Reputation:

how about this:

(?<![\w@])@([\w@]+(?:[.!][\w@]+)*)

I have replaced [^\s<>] with [\w@], which is a bit more restrictive. \w matches letters, numbers, and underscores. If there are any other characters you specifically need to allow, add them to each character class.

This group: (?:\.\w+)* Allows one or more periods to be part of the username, but only if they are followed immediately by word characters. Note that (?:...) is a non-capturing group. It is useful when you want to group things for logical purposes, but don't need to capture the result.

Update: see a working example.

Upvotes: 5

Related Questions