Peter Bushnell
Peter Bushnell

Reputation: 938

regex find a string but not a certain other longer strings containing that same string

I am looking for some regex to locate a string but not several other longer stings containing the string I am looking for. The search is for foo but the result must not contain foo-core, foo core and foogui and fooi.

Searching for:

foo

Result must not contain:

foo-core
foo core
foogui
fooi

Upvotes: 0

Views: 42

Answers (3)

daniel.caspers
daniel.caspers

Reputation: 1756

Check out regular-expressions.com. Here is an excerpt that should help you!

The caret ^ matches the position before the first character in the string. Applying ^a to abc matches a. ^b does not match abc at all, because the b cannot be matched right after the start of the string, matched by ^. See below for the inside view of the regex engine. Similarly, $ matches right after the last character in the string. c$ matches c in abc, while a$ does not match at all.

Please add info in the question if you specifically wanted to black list the other string endings or if you are looking to only end the string with foo!

Upvotes: 0

d0nut
d0nut

Reputation: 2834

Perhaps this is what you're looking for? Your question should be more specific and provide better examples, I think.

foo(?![- ]core|gui|i)

Regex101

Upvotes: 2

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627292

The search is for foo but the result must not contain foo-core, foo core and foogui and fooi.

Here is the regex featuring look-ahead that you can use:

\bfoo(?![- ]core|(?:gu)?i)\b

See demo

The regex matches:

  • \b - beginning of a word
  • foo - foo but only if it is not followed by....
    • [- ] - - or space or core or gui or i
  • \b - make sure we matched a whole word.

Upvotes: 3

Related Questions