Match multiple patterns with a Regex
.net, c#, regex
Solution
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:
^(?=.*(?<date>\d+/\d+/\d+))(?=.*(?<time>\d+:\d+:\d+))(?=.*,(?<pin>\d{4,10}))
Note the `,` in front of the `pin` group. 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):
Pattern for date: \d+/\d+/\d+
Pattern for time: \d+:\d+:\d+
Pattern for pin: (?<=,)\d{4,10}
These will simply give you your desired values as the entire match.
Problem
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 ?