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

Spirals Whirls
  • 523
  • 1
  • 7
  • 24
  • possible duplicate of [Regular expression to match string not containing a word?](http://stackoverflow.com/questions/406230/regular-expression-to-match-string-not-containing-a-word) – HamZa May 26 '13 at 09:29
  • As evidenced from [a previous question](http://stackoverflow.com/q/16731343/20670), 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. – Tim Pietzcker May 26 '13 at 09:29

2 Answers2

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.

melwil
  • 2,478
  • 1
  • 15
  • 31
  • Yes, I can agree yours is slightly more efficient. – melwil May 26 '13 at 11:34
  • 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. – melwil May 26 '13 at 12:01
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

Anirudha
  • 30,881
  • 7
  • 64
  • 81
  • 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.. – Spirals Whirls May 26 '13 at 11:36
  • @SpiralsWhirls you can refer [this](http://www.codeproject.com/Articles/206330/Learning-REGEX-regular-expression-in-the-most-easi) for basic regex tutorial and [this](http://www.codeproject.com/Articles/9099/The-30-Minute-Regex-Tutorial) for more.. – Anirudha May 26 '13 at 11:40
  • @SpiralsWhirls i recommend you `c# in a nutshell` book which has a very simple and thorough explanation of `regex`! – Anirudha May 26 '13 at 11:41
  • 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 – Spirals Whirls May 26 '13 at 11:45
  • 1
    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! – melwil May 26 '13 at 12:28