简体   繁体   中英

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

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

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"

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

^(?<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. ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM