0

I have the following string:

22/04/2020 14:10 by [email protected]

And I wrote the following regex: ^(\d{2,4}\/?)+

Why does group 1 only match the last part of the date and not the whole date, and how can I fix this?

https://regex101.com/r/Pc4McX/1/

4
  • 1
    You are repeating the capturing group, capturing the value of the last iteration. Why not match the format? ^\d{2}/\d{2}/\d{4} Commented Apr 22, 2020 at 14:40
  • Yes, that is also an option, but I was wondering it also could be fixed the way I'm doing it.
    – Drago
    Commented Apr 22, 2020 at 14:41
  • Ok I think I solved it by doing ^((?:\d{2,4}\/?)+)
    – Drago
    Commented Apr 22, 2020 at 14:49
  • You can make it a non capturing group instead ^(?:\d{2,4}\/?)+ and it could possibly also match a / at the end Commented Apr 22, 2020 at 14:49

1 Answer 1

0

Note that regex store all matches in the array - firstly check in your code all matches. I think that the data that you need - already matched - you just need to store it. Following the Regex101 - Full match - will be your arr[0]. If you are using PHP - check this regex-tester = https://www.phpliveregex.com/

My regex :

^(\d+\W)*
1
  • How does this solve my problem? This would match all numbers and A-Za-z0-9_
    – Drago
    Commented Apr 22, 2020 at 14:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.