user1877031
user1877031

Reputation: 31

regex, avoid a line, if a word occurs in it

I am trying to find certain lines in which "like" comes before the word "crusegdf". For example, given the following input:

def shared temp-table w-cruseg no-undo field cruise-id like crusegdf.segment-id
define {1} shared temp-table tt-history no-undo like crusegdf.

I only want to match the second line. But my regex finds both these lines. The regex that I use now is:

(?i)(define|def)(.*?)(temp-table)(.*?)(like)\s*(\bcrusegdf\b)\s*(\.)

Upvotes: 1

Views: 104

Answers (2)

anubhava
anubhava

Reputation: 786291

You can use this negative lookahead regex for matching a like containing like that doesn't contain define before it:

^(?:(?!\bfield\b).)*like.*

RegEx Demo

(?:(?!\bfield\b).)* will match 0 or more any characters that doesn't have field at next position.

Upvotes: 0

Bohemian
Bohemian

Reputation: 425418

You can't use \b to delimit your field name, because your input has field names that contain dots; the dot is not a "word" character.

Instead use a negative look ahead for a dot then more field name:

^(?i)(define|def).*?temp-table.*?like\s*\bcrusegdf\b(?!\.\w).*$

See live demo.

I also removed the unnecessary brackets (your question did not say you needed to capture input).

Upvotes: 1

Related Questions