dtt2012 2013-04-11 09:09
浏览 62
已采纳

正则表达式匹配实际模式之前的空格字符 - PHP

I have this string -: @Harry @Harry are great twins with @Harry

I would want to match @Harry in the above string. But on one condition -:

@Harry should be preceeded by either nothing or a white space character only, same goes for succession too.

So here 3 matches must be found. Currently i am doing this, but in vain -: (\s|^$)\@Harry(\s|^$).

  • 写回答

1条回答 默认 最新

  • duangao7133 2013-04-11 09:13
    关注

    ^$ means an empty string.
    [\s means more than a whitespace (source) so you should just use a whitespace :)] <= Unnecessary

    So, without taking the whitespace as part of the match:

    "/(?<=^|\s)@Harry(?=\s|$)/"
    

    Edit:
    Source for the lookarounds.
    Also, if you want to match the whitespaces, remove those lookarounds.

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

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应