Robert1
Robert1

Reputation: 23

Regex to match a numeric token, but only if not part of another word

Using C# Regex I'm trying to match a token which contains the @ symbol. For instance:

Your salary is @1 for this month.

I can't use a pattern like \b@1\b because \b matches the beginning and ending of a word and @1 is correctly not recognised as a word.

What pattern can I use to match tokens like this i.e @1, @2 etc?

Upvotes: 2

Views: 215

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627020

You can use look-arounds (?<!\w) (=no word character allowed right before) and (?!\w) (=no word character allowed right after).

(?<!\w)@\d+(?!\w)

See regex demo

Note that \d+ matches 1 or more digits.

Upvotes: 2

Related Questions