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.

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

报告相同问题?

悬赏问题

  • ¥35 VBA-JSON中文乱码报错
  • ¥50 dac adc的检定规程
  • ¥20 MIT控制器能控制不稳定系统吗
  • ¥15 公司代码X对业务伙伴X无效,处理方法?
  • ¥15 微信内链接跳转到浏览器打开怎么实现
  • ¥15 三角波可以直接加施密特电路整形到矩形波吗实物
  • ¥15 html,php,在使用html请求php文件时发生了错误,无法请求到php文件读取数据库并用javascript进行数据显示,刷新
  • ¥15 touchsocket udp组播
  • ¥20 MAC怎么安装Silverlight 插件?以及安装了怎么启用
  • ¥15 gis系统开发出现命名空间“ESRI.ArcGIS”中不存在类型或命名空间名“Analyst3D”报错