Ajinkya Kale
Ajinkya Kale

Reputation: 147

C# regex to find all the terms that start with @ but ignoring terms with white space

I have a text that may contain terms starting with @, I need to extract only the terms which are the whole word and not the terms with white space. The @terms will always appear before action:

example: command @abc @xyz @abc xyz action: Hello world

The output should be: abc, xyz

This is what I have been working with but I am having difficulty in extending it.

(?<!\w)@.*?\s

The output I am getting with this is as follows (colored in grey)

command @abc @xyz @abc xyz action: hello world

Upvotes: 0

Views: 70

Answers (1)

Nick
Nick

Reputation: 147166

You need to only allow matching if an @xxx is followed by another @xxx or action:. You can do that with this regex, which uses a positive lookahead for those two conditions:

(?<!\w)@\S+(?=\s+(?:@|action:))

Demo on regex101

Upvotes: 2

Related Questions