Reputation: 47
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.
Upvotes: 0
Views: 97
Reputation: 626689
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.
Upvotes: 1