0

My RegExpression:

((^|\s)(clever)($|\s))

It finds "clever" in the string:

  • clever or not
  • yahoo clever

but it doesn't find "clever" in this string:

  • what means cleverness

I don't want to bother you with the three other RegExp variations of my line above but I tried different approaches already but can't make it work.

I am filtering terms in a table to cluster them into defined groups. I am looking for the adjective "clever". I dont want to find strings where clever is part of another word, in example "MacLever" or "alcleveracio".

Andy A.
  • 1,392
  • 5
  • 15
  • 28
seoux
  • 1
  • 1

3 Answers3

1

Try this :

((^|\s)(clever))

Your regex contains ($|\s) will force clever to be before a space or at the end of the string.

SelVazi
  • 10,028
  • 2
  • 13
  • 29
0

Try using ^(.*\W)?(clever)(\W.*)?$instead. \W matches any non-word character, so this will enforce that any string before "clever" include a nonword character at the end (and vice versa for the end.

You can plug it into https://regex101.com/ to see how it is working and test it out.

0

You can use the word boundary \b.

\bclever\w*\b

or maybe better (no capitals allowed)

\bclever[a-z]*\b

If "clever" should be either at the beginning or at the end:

\b([a-zA-Z]+)?clever(?(1)|[a-z]*)\b
  1. \b beginig of the string
  2. ([a-zA-Z]+) at least one character
  3. ? match even group is empty
  4. clever matches the characters
  5. (?(1) starts a condition, depends on group 1
  6. |[a-z]*) if group matches, there doesn't may be any chars, else ( | ) there may be any lower case chars ( [a-z]* )
  7. \b the final word boundary

Test and visualizing: Debuggex Demo
Infos about If-Then-Else


Regex image 1

Regex image 2

(visulized by Regulex)

Test it on regex101

Andy A.
  • 1,392
  • 5
  • 15
  • 28