Barry
Barry

Reputation: 83

Regex match all whitespace sepearated tokens that are in a specific pattern

For example text

Lorem Ipsum is. simply-dummy text/of 
the printing and'typesetting  industry

I'd like to all the token separated by whitespace characters \s that is in [a-zA-Z] So the matches will be

Lorem
Ipsum
the
printing
industry

I know I can first split and then match every token using [a-zA-Z], but I'd like to know whether it's possible to use only 1 regular expression to achieve this.

Upvotes: 0

Views: 478

Answers (2)

The fourth bird
The fourth bird

Reputation: 163362

You can assert whitespace boundaries to the left and right using negative lookarounds asserting not a non whitespace char.

(?<!\S)[a-zA-Z]+(?!\S)

Regex demo

Upvotes: 1

zipa
zipa

Reputation: 27869

This one should do it:

(?<=\s|^)([A-Za-z]+)(?=\s|$)

Example on Regex101.

Upvotes: 0

Related Questions