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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了