PHP Regexp 负前瞻 [英] PHP Regexp negative lookahead

查看:39
本文介绍了PHP Regexp 负前瞻的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试匹配所有用 { } 包裹的单词,但不匹配带有_loop"的单词.我看不出我的 reg 表达式哪里出错了.

I'm trying match all words wrapped with { } but not the words with "_loop". I can't see where I'm going wrong with my reg expression.

 $content   = '<h1>{prim_practice_name}</h1><p>{prim_content}</p><p>Our Locations Are {location_loop}{name} - {state}<br/>{/location_loop}</p>';
 $pattern = '/\{(\w*(?!\_loop))\}/';

推荐答案

发生这种情况是因为 \w* 在检查之前吃掉"了停止词_loop",以防止您应该先检查单词(在 \w* 之前)),如下所示:

This happens because \w* "eats" the stopping word "_loop" before your check, to prevent that you should check the word first (before \w*), like the following:

$pattern = '/\{((?!\w*_loop\})\w*)\}/';

或者你可以使用:?<! :

$pattern = '/\{(\w*(?<!_loop))\}/';

这篇关于PHP Regexp 负前瞻的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆