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?
source to share
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
source to share