Neil
Neil

Reputation: 6039

Regular expression to match but exclude from the selection

I am trying to get the tokens in the message text so that they can be string replaced by the actual values. My text is

Hello &worls nice &to & Here &ok.

The expression (&\w+?)\W gives me

worls<whitespace>
to<whitespace>
ok.

However I need to exclude the last letter i.e whitespace or period

Testing link http://regexr.com?33nrg

Upvotes: 0

Views: 1700

Answers (2)

Achrome
Achrome

Reputation: 7821

This should work.

&\S+[^\.\s]

Or, as Anton suggested.

&\S+\b

Upvotes: 1

Anton Kovalenko
Anton Kovalenko

Reputation: 21507

Test for word boundary (\b) instead of looking for non-word character \W.

Also it would be fine to learn how to work with regexp match groups in your tool or language of choice, so you can retrieve and use a substring matching any subexpression.

Upvotes: 2

Related Questions