PHP Regexp negative lookahead
Asked Answered
A

1

9

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))\}/';
Austinaustina answered 25/3, 2013 at 23:50 Comment(1)
You are looking for a lookbehind ?<!.Evaporate
K
8

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))\}/';
Klipspringer answered 26/3, 2013 at 0:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.