douxieqiu0651 2016-03-28 16:00
浏览 28
已采纳

使用右括号使正则表达式工作

I have the following regex:

$regex = '/(?:(?:\b(?:pp|[Ww]ars|[\.!?)]?\s*f|vs)[\.!?)]\s))\Z/mu';

It matches with:

$string = 'Wars! ';

But I want it to match with the following scenario:

$string = 'Wars!) ';

I have added the closing bracket there but obviously I'm doing something wrong.

  • 写回答

1条回答 默认 最新

  • douzhong5902 2016-03-28 16:06
    关注

    You can add a * quantifier to the last [.!?)] to match zero or more occurrences:

    (?:(?:\b(?:pp|[Ww]ars|[.!?)]?\s*f|vs)[.!?)]*\s))\Z
                                               ^
    

    See the regex demo

    Also note that a . inside a character class does not have to be escaped to match a literal dot symbol.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java 判断某个数 区间是否存在
  • ¥15 appium控制多个雷电模拟器问题
  • ¥15 C# iMobileDevice
  • ¥15 谁会做这个啊#ensp#Boson NetSim
  • ¥15 如何编写针对TPS6503320FRGE型号的电源管理芯片的编程代码?
  • ¥15 设计简单目录管理系统,要满足以下内容
  • ¥15 关于九十度混合耦合器信号分析问题
  • ¥15 Cesm如何关闭不用的模块呢
  • ¥15 vue2两层数据导出为excle
  • ¥15 有人能帮我做一下这个项目吗