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 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果