Search code examples
phppreg-match-all

preg_match_all for dates with different format


I have a preg_match to get dates from a string:

preg_match_all('/\d{1,2}\.\d{1,2}\.\d{2,4}/',$myString,$dates);

This will get dates with the syntax:

01.01.2022
1.01.2022
1.1.2022
1.1.22
...

Now I have a date format like this:

25 März 2023
or
25 March 2023

How can I get this?


Solution

  • You could write the pattern as:

    \b\d{1,2}(?:\.\d{1,2}\.|\h+\p{Lu}\p{Ll}+\h+)(?:\d\d|\d{4})\b
    

    Explanation

    • \b A word boundary
    • \d{1,2} Match 1-2 digits
    • (?: Non capture group
      • \.\d{1,2}\. Match . 1-2 digits .
      • | Or
      • \h+\p{Lu}\p{Ll}+\h+ Match any uppercase char followed by 1+ lowercase chars between 1+ horizontal whitespace chars
    • ) Close the non capture group
    • (?:\d\d|\d{4}) Match 2 or 4 digits
    • \b A word boundary

    See a regex demo and a PHP demo.

    $re = '/\b\d{1,2}(?:\.\d{1,2}\.|\h+\p{Lu}\p{Ll}+\h+)(?:\d\d|\d{4})\b/u';
    $str = '01.01.2022
    1.01.2022
    1.1.2022
    1.1.22
    ...
    Now I have a date format like this:
    
    25 März 2023
    or
    25 March 2023';
    
    preg_match_all($re, $str, $matches);
    print_r($matches[0]);
    

    Output

    Array
    (
        [0] => 01.01.2022
        [1] => 1.01.2022
        [2] => 1.1.2022
        [3] => 1.1.22
        [4] => 25 März 2023
        [5] => 25 March 2023
    )