0

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

2
  • possible duplicate of Regular expression to match string not containing a word? Commented May 26, 2013 at 9:29
  • As evidenced from a previous question, you have a very rudimentary understanding of how lookaround assertions work. You should read the tutorial that can be found under the link you posted, especially the sections on lookaround assertions. They do "discuss complicated stuff", but you first need to understand the simple stuff. Commented May 26, 2013 at 9:29

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks dear.. the link you provide is one I have mentioned too .. but this is not clearing my mind .. Actually regex has made me confused and the pattern I guess does never work.. I don't understand how to formulate a regex..
@SpiralsWhirls you can refer this for basic regex tutorial and this for more..
@SpiralsWhirls i recommend you c# in a nutshell book which has a very simple and thorough explanation of regex!
I have read the "Regular Expressions cookbook, Second editions" and tried to memorize all the patterns but no help :( ok I am going to find this book C# in a nutshell
My best tip for leaning regex is just to try and use it for everything, even if it isn't the best solution. The cases where it isn't the best solution often spurs you to learn new things and actually grasp concepts you have been stuggling with. Good luck!
2
.*\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.

2 Comments

Yes, I can agree yours is slightly more efficient.
Both our answers accomplish the same, but @Anirudh's answer is slightly more efficient, and also may be easier to read. I'm not a big fan of quantifiers in lookaheads, though. The difference in efficiency is negligible anyway, since the regex engine will have to do the same for both regexes. I'll say make Anirudh's answer the accepted one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.