Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

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.

like image 447
user2802729 Avatar asked Sep 16 '25 06:09

user2802729


1 Answers

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.

like image 183
p.s.w.g Avatar answered Sep 17 '25 19:09

p.s.w.g