0

I have a regex witch searches for dates (0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](20)\d\d.

The problem is that it also returns matches where the match is within another string like 10.10.10.2019 it matches 10.10.2019 as a date. Tried with \b at the beginning and end but no luck. Also used ^ and $ but still no luck.

0

1 Answer 1

1

You need to use lookarounds to only match in between whitespaces or start/end of string:

(?<!\S)(?:0?[1-9]|[12][0-9]|3[01])([- /.])(?:0?[1-9]|1[012])\k<1>20\d\d(?!\S)
^^^^^^^                                                                ^^^^^^

See the regex demo

I also suggest to check for the identical separators by capturing the first separator with ([- /.]) and then using \k<1> (unambiguous) backreference to match the same value.

Sign up to request clarification or add additional context in comments.

1 Comment

The regex you supplied doesn't recognize this date format 1-1-2016

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.