How to match words with common prefix in PHP?
I need to write a 开发者_StackOverflow中文版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.
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
I think this gets you there:
/\<word(|s|ing)\>/
\b(accounting|accounts|account)\b
I might be wrong, but I don't think "/\b*word*\b/"
gets word, instead it actually matches things like '''''wor'
or ||worddddd|
, if you want word and its variant, try:
/\bword[^\b]+\b/i
精彩评论