C# 使用正则表达式匹配多个模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14015458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Match multiple patterns with a Regex
提问by Rafael Adel
I have 3 groups : time, date and pin. And i can have this line to match this lines :
我有 3 组:时间、日期和图钉。我可以用这条线来匹配这条线:
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
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/2016/2012,3,26/2016/26/2016/26/26/26/26/2012 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.
我想26/06/2012
作为date
组,33:06:12
作为time
和3412234
作为pin
组匹配。
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 ?
我的问题是,无论线条形式如何,如何匹配时间、日期和引脚组?
采纳答案by Martin Ender
If you don't want to validate the pattern at the same time, you can use lookaheadsstarting 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).
注意,
前面的pin
组。否则,您可能会冒着找到年份作为 pin 的风险(因为它也是 4 位数字)。
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.
这些只会在整个匹配中为您提供所需的值。
回答by Martin Ender
Just add |
between your expressions:
只需|
在表达式之间添加:
(?<date>[\d]+/[\d]+/[\d]+)|(?<time>[\d]+:[\d]+:[\d]+)|(?<pin>(?<=[^/])[\d]{4,10})