0

I know that perl compatible regex can do this easily by using the "lookaround" technology, but I only have posix regex on my hand which does not support the "lookaround", so my question is that can I find A but not B on a line only using posix regex.

Here is a simple example:

I want to find a "foo" but no "bar" on a line,

this has a foo           # match
i can haz foo            # match
but i haz foo and bar    # NOT match

and here is a similar question

Community
  • 1
  • 1
ethandu7
  • 43
  • 7

1 Answers1

0

The best way (whatever the language or tool you use) is to test the two patterns (or the two strings) separately. Example with sed:

sed -n '/foo/{/bar/!p}'

It's short, simple and efficient.

To do the same with one ERE pattern, you must write:

^([^b]|b([^a]|$|a([^r]|$)))*foo([^b]|b([^a]|$|a([^r]|$)))*$

Do you think it's a better choice? (and it is a three letters word)

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125