user2802729
user2802729

Reputation: 77

What regex pattern will match a whole word that contains certain characters?

I want to match whole words that contain certain letters/characters. For example the pattern would match all words that contain the letter l and a, such as car, patrol, left, etc. but it wouldn't match words like boom, turnover, digit, etc.

Upvotes: 4

Views: 14474

Answers (1)

p.s.w.g
p.s.w.g

Reputation: 149088

A pattern like this should work:

\b(?=\w*[al])\w+\b

This will match one or more 'word' characters (letters, digits, or underscores) only of it contains an a or l character. The \b around it will match the boundary of that word (e.g. where the next character is a non-word character), so it will capture the entire word.

Upvotes: 7

Related Questions