Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match words with common prefix in PHP?

I need to write a regex to match the word. To find exact word can be done using /\bword\b/ pattern.

But I want the pattern to find word, words, wording and so on.

for example i want to write a pattern to find terms account, accounting, accounts and accountant in a paragraph string.

like image 581
daron Avatar asked Nov 28 '25 08:11

daron


2 Answers

To just match your keyword optionally followed by s, ing, ant, you can use:

/\b($word(?:|s|ing|ant))\b/i

see it

If you want to allow any optional letters as suffix you can use:

/\b($word\w*)\b/i
like image 176
codaddict Avatar answered Nov 30 '25 21:11

codaddict


I think this gets you there:

/\<word(|s|ing)\>/
like image 21
wallyk Avatar answered Nov 30 '25 22:11

wallyk