Search code examples
regexdatepattern-matchingexpresso

REGEX: How to find a date format after a specific keyword?


I am scratching my head over this. For example I have:

Date
sometext
somtext
27-7-2013

What I want to do is I want to get the first date format string pattern after the Date keyword. Here is what I currently have:

(?i)(?<=date.*)\d+-\d+\d+

But unfortunately, I am getting nothing and if I just try to get all the string after the date keyword, e.g.

(?i)date.*

I am only getting the result as:

Date[CR]

I am using Expresso to test out my regular expressions. I am pretty new with using regex so I am not familiar with how to do things and stuff.

Please help! Thanks!


Solution

  • I would not use a lookbehind assertion in this case, also unlimited length lookbehinds are (I think) only supported by .net.

    I would use a capturing group instead:

    (?is)date.*?(\d{1,2}-\d{1,2}-\d{4})
    

    You will find the date in the first capturing group.

    See it here on Regexr

    (?is) is enabling the case independent matching mode i and the single line mode s, that makes the . also match newline characters.

    .*? is matching lazily any characters.

    (\d{1,2}-\d{1,2}-\d{4}) matches one or two digits, a dash, one or two digits, a dash, then 4 digits.