简体   繁体   English

如何在正则表达式中使用“ OR”?

[英]How to use 'OR' in regular expressions?

I want to have a regular expression that accepts these forms : 我想有一个接受这些形式的正则表达式:

23456KW33C3 23456KW33C3

23456KW33 23456KW33

23456K 23456千

23456 23456

23456W33C3 23456W33C3

23456W33 23456W33

I wrote these two regex 我写了这两个正则表达式

/(?i)^2\d{4}K?(M|(W33)(C3)?)?$/i

/(?i)^2\d{4}K?(M|(W33)|(W33C3))?$/i

They accepts all forms but not 23456KW33C3 !!! 他们接受所有形式,但不接受23456KW33C3

I use it in if expression : 我在if表达式中使用它:

if (preg_match('/(?i)^2\d{4}K?(M|(W33)(C3)?)?$/i' ,$text))

What's wrong with them ? 他们怎么了 ?

You expression works fine, you might just want to remove the start and end chars and it would pass 23456KW33C3 too. 您的表达式工作正常,您可能只想删除开始和结束字符,它也将通过23456KW33C3

2\d{4}k?(m|(w33)(c3)?)?

This link helps you to modify/update your expressions as you wish. 该链接可帮助您根据需要修改/更新表达式。

在此处输入图片说明

I'm not quite sure what you would like to match. 我不太确定您想搭配什么。 You might want to simplify your expression and reduce the restrictions that it has. 您可能想要简化您的表达并减少它的限制。


If you wish to write your expressions with OR, this expression shows how you might want to do so: 如果您希望使用OR编写表达式,则此表达式说明了您可能希望这样做:

2\d{4}(k|)|(m|w33)|(c3)

You can add them in between the capturing groups that you have, to swipe your desired outputs from left to right. 您可以将它们添加到您拥有的捕获组之间,以从左向右滑动所需的输出。 Then, if you wanted, after that you can add more boundaries. 然后,如果需要,之后可以添加更多边界。

在此处输入图片说明

RegEx Circuit RegEx电路

This link helps you to visualize your expressions: 链接可帮助您可视化表达式:

在此处输入图片说明

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

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