SDV
SDV

Reputation: 33

How to exclude a specific match from my regex extraction?

I have the following regex pattern:

(?<error>([0-9a-z])+type=)

And the following sample events:

auid=3000964000type=DAEMON_START
footype=DAEMON_START
asfasdatype=DAEMON_START

I want to exclude footype from the extraction so that it matches only the first and last sample events.

I'm aware there's answers here that mention negative lookahead, but I'm not able to make it work.

Could you help me make a regex pattern that works as intended?

Upvotes: 1

Views: 68

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626794

You can use

(?<error>[0-9a-z]+(?<!\bfoo)type=)

See the regex demo. Details:

  • (?<error> - start of a named capturing group "error":
    • [0-9a-z]+ - one or more digits or ASCII lowercase letters
    • (?<!\bfoo) - a negative lookahead that fails the match if there is foo preceded with a word boundary immediately to the left of the current location
    • type= - a literal type= text
  • ) - end of the named group.

Upvotes: 1

Related Questions