使用正则表达式或运营商来解决2条件 [英] Using Regex OR operator to solve 2 conditions

查看:144
本文介绍了使用正则表达式或运营商来解决2条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想2定期EX pressions合并成1 OR运算符: |

I am trying to combine 2 regular expressions into 1 with the OR operator: |

我有一个检查一个字母后跟8位数的匹配:

I have one that checks for match of a letter followed by 8 digits:

Regex.IsMatch(s, "^[A-Z]\d{8}$")


我有另一种检查只需9个数字:


I have another that checks for simply 9 digits:

Regex.IsMatch(s, "^\d{9}$")


现在,而不是做的:


Now, Instead of doing:

If Not Regex.IsMatch(s, "^[A-Z]\d{8}$") AndAlso
   Not Regex.IsMatch(s, "^\d{9}$") Then 
    ...
End If


我想我可以简单地做:


I thought I could simply do:

If Not Regex.IsMatch(s, "^[A-Z]\d{8}|\d{9}$") Then
    ...
End If


显然我没有正确地结合两者,显然我在正规前pressions可怕。任何帮助将是非常美联社preciated。


Apparently I am not combining the two correctly and apparently I am horrible at regular expressions. Any help would be much appreciated.

对于那些想知道,我也采取了一眼如何结合在正则表达式 2的条​​件和更多的,我还抓我的头。

And for those wondering, I did take a glance at How to combine 2 conditions and more in regex and I am still scratching my head.

推荐答案

如何使用 ^ [A-Z0-9] \\ D {8} $

这篇关于使用正则表达式或运营商来解决2条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆