简体   繁体   English

正则表达式匹配多个组中的任何一个

[英]RegEx match on any of multiple groups

I'm not sure if this is possible, but I would like to match on multiple regex groups 我不确定这是否可行,但我想在多个正则表达式组上进行匹配

(^[0-9]) (^[$][0-9]) (^[$]{2}[0-9])

It would match the string if the first character is number, or if the first character is a $ followed by a number, or if the first two characters are a $ followed by a number. 如果第一个字符是数字,或者第一个字符是$后跟数字,或者前两个字符是$后跟数字,则它将与字符串匹配。

Example strings that would match: 匹配的示例字符串:

15271%
$3C001%
$$8244150928223C001%

Can this be done in one go, or would I have to check each match individually? 可以一次性完成吗,还是我必须单独检查每场比赛?

Any help is appreciated. 任何帮助表示赞赏。 Thanks! 谢谢!

You could use: 您可以使用:

^\d.*|^\$\d.*|^\$\$\d.*


try {
    if (Regex.IsMatch(subjectString, @"\A(?:^\d.*|^\$\d.*|^\$\$\d.*)\z", RegexOptions.Multiline)) {
        // Successful match
    } else {
        // Match attempt failed
    } 
} catch (ArgumentException ex) {
    // Syntax error in the regular expression
}

You can make make use of the pipe symbole | 您可以使用管道符号| to achieve that. 实现这一目标。 It basically behaves like an "or" in your regex pattern. 在您的正则表达式模式中,它的行为基本上像一个“或”。

For example: 例如:

(banana|apple)

would match both "banana" and "apple". 将同时匹配“香蕉”和“苹果”。

In your case, you can also use a pattern like this 在您的情况下,您也可以使用这样的模式

(\${0,2}\d.+)

to match all options: without $, with one $ and with two $. 匹配所有选项:不带$,带一个$和带两个$。

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

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