Regex中发生了什么匹配

本文关键字:什么 发生了 Regex | 更新日期: 2023-09-27 18:28:27

让我们想象一下我有一些正则表达式模式:

(a)|(b)|(c)

如何识别触发了什么匹配?有某种匹配指数吗?

我可以检查所有组的null,或者将全局值字段与组值f"行进行检查,但这有点糟糕,并增加了复杂性。正则表达式没有某种有限状态值吗?

Regex中发生了什么匹配

当然可以:

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

你也可以命名你的小组,让它更容易跟随:

(?<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