Unexpected Regex result, 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 dates are from line 3 and onwards.

I wrote this regex ((?:Jul|Aug)\W\d+\W+\d+)

This finds all dates in the text.

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

But the results are not what I expected. What am I doing wrong here?

regex101 reference

+1


source to share


4 answers


You can use the ^

and bindings $

to force your regex engine to match the beginning and end of the line and use .*

in your negative prediction:

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

      

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



If you want to opt out of this, you can also use a negative look and feel:

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

      

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

0


source


Just use anchors and word border.

"(?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

+1


source


This should work for you:

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

      

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

0


source


^(?<month>\w{3}) (?<day>\d{1,2}), (?<year>\d{4})(?! - \w{3}\s+\d{1,2}, \d{4})[ \t]*(?<data>.*?)$

It's a bit verbose but seems to work fine.;)

0


source







All Articles