php - How to match multiple (N) spaces in regex's lookarounds? -


i have regex (?<={% start %}).*?(?={% end %}) matches between 2 custom tags.

the problem if there spaces inside tags (e.g., "{%    start %}") , add \s+? condition, regex fails. following code not work: (?<={%\s+?start\s+?%}).*?(?={%\s+?end\s+?%}) , i'm getting error in php:

preg_match_all(): compilation failed: lookbehind assertion not fixed length @ offset 25 

the same regex works if remove lookahead/lookbehind: ({%\s+?(start|end)\s+%}).

please advise.

description

try permlink

[{]%\s*?\b([^}]*start[^}]*)\b\s*?%[}]\s*?\b(.*?)\b\s*?[{]%\s*\b([^}]*end[^}]*)\b\s*%[}] 

this match text inside {% , %} brackets, , automatically trim text before putting value groups.

group 0 gets entire matching string

  1. gets start tag text
  2. gets inner text
  3. gets end tag text

enter image description here

disclaimer

this have edge cases regex fail if have complex data nested sub, if using regex not best tool task.

summary

[{]%\s*?\b([^}]*start[^}]*)\b\s*?%[}]\s*?\b(.*?)\b\s*?[{]%\s*\b([^}]*end[^}]*)\b\s*%[}] char class [{] matches 1 of following chars: { % literal `%` \s 0 infinite times [lazy] whitespace [\t \r\n\f]  \b word boundary: match in between (^\w|\w$|\w\w|\w\w) 1st capturing group ([^}]*start[^}]*)  negated char class [^}] infinite 0 times matches char except: } start literal `start` negated char class [^}] infinite 0 times matches char except: } \b word boundary: match in between (^\w|\w$|\w\w|\w\w) \s 0 infinite times [lazy] whitespace [\t \r\n\f]  % literal `%` char class [}] matches 1 of following chars: } \s 0 infinite times [lazy] whitespace [\t \r\n\f]  \b word boundary: match in between (^\w|\w$|\w\w|\w\w) 2nd capturing group (.*?)  . 0 infinite times [lazy] character (except newline)  \b word boundary: match in between (^\w|\w$|\w\w|\w\w) \s 0 infinite times [lazy] whitespace [\t \r\n\f]  char class [{] matches 1 of following chars: { % literal `%` \s infinite 0 times whitespace [\t \r\n\f]  \b word boundary: match in between (^\w|\w$|\w\w|\w\w) 3rd capturing group ([^}]*end[^}]*)  negated char class [^}] infinite 0 times matches char except: } end literal `end` negated char class [^}] infinite 0 times matches char except: } \b word boundary: match in between (^\w|\w$|\w\w|\w\w) \s infinite 0 times whitespace [\t \r\n\f]  % literal `%` char class [}] matches 1 of following chars: } 

php example

with sample text {% start %} sample text 1 {% end %}{% start %} sample text 2 {% end %}

<?php $sourcestring="your source string"; preg_match_all('/[{]%\s*?\b([^}]*start[^}]*)\b\s*?%[}]\s*?\b(.*?)\b\s*?[{]%\s*\b([^}]*end[^}]*)\b\s*%[}]/i',$sourcestring,$matches); echo "<pre>".print_r($matches,true); ?>  $matches array: (     [0] => array         (             [0] => {% start %} sample text 1 {% end %}             [1] => {% start %} sample text 2 {% end %}         )      [1] => array         (             [0] => start             [1] => start         )      [2] => array         (             [0] => sample text 1             [1] => sample text 2         )      [3] => array         (             [0] => end             [1] => end         )  ) 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -