简体   繁体   English

在Regex中发生了什么匹配

[英]What match is occured in Regex

Let's imagine i have some regex pattern: 让我们想象一下我有一些正则表达式模式:

(a)|(b)|(c)

How to identify what match has been triggered? 如何识别已触发的匹配? Is there some kind of index of match? 有某种匹配指数吗?

I can check all groups for null or compare global Value field with group Value fields or separate pattern to many and check them through line of " if ", but its kinda crappy and gives additional complexity. 我可以检查所有组为null或将全局值字段与组值字段或单独的模式比较为多个并通过“ if ”行检查它们,但它有点蹩脚并且给出了额外的复杂性。 Is regex don't have some finite state value? 正则表达式没有一些有限的状态值吗?

Sure you can do this: 当然你可以这样做:

match.Groups[1].Success // true or false
match.Groups[2].Success // true or false
match.Groups[3].Success // true or false

You could also name your groups to make it easier to follow: 您还可以为群组命名,以便更轻松地遵循:

(?<foo>a)|(?<bar>b)|(?<baz>c)
match.Groups["foo"].Success // true or false
match.Groups["bar"].Success // true or false
match.Groups["baz"].Success // true or false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM