Reputation: 77
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
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