3

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.

codaddict
  • 445,704
  • 82
  • 492
  • 529
daron
  • 33
  • 4

4 Answers4

4

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
codaddict
  • 445,704
  • 82
  • 492
  • 529
  • the second one for any variation worked for me. thanks. @andread \w was missing instead of . in your pattern, anyways thanks again to all – daron Dec 03 '10 at 06:56
  • @daron: You are welcome. Also since you are new to SO, I would like to tell you that you can accept the answer that helped you the most by clicking on the right mark next to the answer :) – codaddict Dec 03 '10 at 06:59
  • how to use the same expression for mysql. I tried SELECT * FROM table WHERE word REGEXP '/\b($word\w*)\b/i'; this didn't worked – daron Dec 03 '10 at 20:57
1

I think this gets you there:

/\<word(|s|ing)\>/
wallyk
  • 56,922
  • 16
  • 83
  • 148
0
\b(accounting|accounts|account)\b
Mike Clark
  • 10,027
  • 3
  • 40
  • 54
0

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
Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
  • ignore those * i was actually trying to bold the word. your solution instead of looking for a word actually matched everything after the first match for word. – daron Dec 03 '10 at 06:53
  • 1
    `[^\b]` won't work - `\b` is not interpreted as a word boundary inside a character class. The negation of `\b` is `\B`, but even this won't work here - it doesn't make any sense to repeat a zero-length assertion, and of course it will never be possible to match a `\b` right after a `\B`. – Tim Pietzcker Dec 03 '10 at 07:36