prettyvoid
prettyvoid

Reputation: 3686

Regex negative lookahead, unexpected results

I have the following text:

Jul 31, 2015 - Aug 27, 2015
Jul 31, 2015 - Aug 27, 2015
Aug 27, 2015
Jul 31, 2015 Data1
Jul 31, 2015 Data2
Jul 31, 2015
Jul 31, 2015

I want to match all dates except those that have a date next to them. So basically the dates from Line 3 and forward.

I wrote this regex ((?:Jul|Aug)\W\d+\W+\d+) This finds all the dates in the text.

Then to achieve what I want I thought that I have to use a negative lookahead like so ((?:Jul|Aug)\W\d+\W+\d+(?! - Aug 27, 2015))

But the results is not what I was expecting. What am I doing wrong here?

regex101 link

Upvotes: 1

Views: 102

Answers (4)

Kasravnd
Kasravnd

Reputation: 107287

You can use anchors ^ and $ to force your regex engine to match from start and end of string and use .* in your negative look ahead :

^((?:Jul|Aug)\W\d+\W+\d+)(?!.*(?:Jul|Aug)\W\d+\W+\d+).*(?:$|)

see demo https://regex101.com/r/dT6fY3/2

If you want to refuse that too you can use a negative look-behind too :

(?<! - )((?:Jul|Aug)\W\d+\W+\d+)(?!.*(?:Jul|Aug)\W\d+\W+\d+)

see demo https://regex101.com/r/eX8yF8/8

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174696

Just use anchors and word boundary.

"(?m)^(?:Jul|Aug)\\W\\d+\\W+\\d+\\b(?! - Aug 27, 2015$).*"

or

"(?m)^(?:Jul|Aug)\\W\\d+\\W+\\d+\\b(?! - (?:Jul|Aug)\\W\\d+\\W+\\d+$).*"

DEMO

Upvotes: 1

Xupicor
Xupicor

Reputation: 71

^(?<month>\w{3}) (?<day>\d{1,2}), (?<year>\d{4})(?! - \w{3}\s+\d{1,2}, \d{4})[ \t]*(?<data>.*?)$ This is a little verbose, but seems to work fine. ;)

Upvotes: 0

d0nut
d0nut

Reputation: 2834

This should work for you:

(?<!.)(?:Jul|Aug)\W\d+\W+\d+\b(?! - Aug 27, 2015)

Edit: Sorry, I read your question as "match 3 lines" not "line 3 and forward"

Upvotes: 0

Related Questions