Reputation: 147
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
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:))
Upvotes: 2