Spirals Whirls
Spirals Whirls

Reputation: 553

finding substring with regex

I am trying to match the substring My Links with negative lookahead like this

      \b(?!My Links)\b

i tried this one too

        .*\b(?!My Links)\b

but it matches everything even if I enter My Links. I want to reject any line containing this Substring. Also I must need reference to material which discusses the lookaheads in details. As I tried but there are only recipes of regex and no explaination as to how this works. and checked this link but it is very simple does not discuss complicated stuff.

Edit The sub string must occur on word boundaries

Upvotes: 0

Views: 92

Answers (2)

Anirudha
Anirudha

Reputation: 32797

You can use this regex

^(?!.*\bMy Links\b).*$

This would match the lines which don't have My Links in it.


You can refer this for more in-depth info on lookarounds

Upvotes: 2

melwil
melwil

Reputation: 2553

.*\b(?!My Links)\b

In this regex, you are looking for any text .*, followed by a word boundary that is not followed by My Links. This will always be true on the last word boundary on a line, and will therefore match anything.

^((?!\bMy Links\b).)+$

This one should do what you want, basically it is looking at the whole string, as specified with the ^ and $ anchors. It looks inside that string for one or more, +, occurrences of a character that doesn't start the string My Links. The word bondaries are also in there.

My Links
here are some of My Links to test
This should not match ehmMy Links though
Not this one My Linkseither

The first two lines will not match here, while the last two will.

Upvotes: 2

Related Questions