问题
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