douzhicong5965 2019-05-12 10:30
浏览 296

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

I want to have a regular expression that accepts these forms :

23456KW33C3

23456KW33

23456K

23456

23456W33C3

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 !!!

I use it in if expression :

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

What's wrong with them ?

  • 写回答

1条回答 默认 最新

  • douzhang5199 2019-05-12 16:33
    关注

    You expression works fine, you might just want to remove the start and end chars and it would pass 23456KW33C3 too.

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

    This link helps you to modify/update your expressions as you wish.

    enter image description here

    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:

    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.

    enter image description here

    RegEx Circuit

    This link helps you to visualize your expressions:

    enter image description here

    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线