PHP Regexp negative lookahead

ぃ、小莉子 提交于 2019-12-23 08:48:01

问题


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))\}/';

回答1:


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*)\}/';

or you can use: ?< ! :

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


来源:https://stackoverflow.com/questions/15626955/php-regexp-negative-lookahead

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!