I have 3 groups : time, date and pin. And i can have this line to match this lines :
26/06/2012 33:06:12a_user_logged_in,3412234,2,3,512,3
33:06:12a_user_logged_in,3412234,2,3,512,3,26/06/2012
26/06/2012 a_user_logged_in_at,33:06:12,3412234,2,3,512,3
I want to match 26/06/2012 as the date group, 33:06:12 as the time and 3412234 as the pin group.
I’ve succeeded in doing so but only the line must be in a certain pattern like the first one
(?<date>[\d]+/[\d]+/[\d]+) (?<time>[\d]+:[\d]+:[\d]+)([ |,][a-zA-z]*)+,(?<pin>[\d]{4,10}).+
But when i apply this pattern to the other two lines forms, it didn’t match.
My question is, how to match time, date and pin groups no matter what was the line form ?
If you don’t want to validate the pattern at the same time, you can use lookaheads starting from the beginning of the string. Since they don’t actually consume anything, the engine jumps back to the start after completing one lookahead. Hence, the order of the three matches does not matter:
Note the
,in front of thepingroup. Otherwise you risk that the year is found as the pin (since it is also 4 digits).But then again, for the sake of readability of your code you might want to just split that into three patterns (this also avoids capturing so it might not even be that much slower):
These will simply give you your desired values as the entire match.