php - Simplify & Fix Regex -


i'm new working regular expressions , wondering if following regex somehow simplified...

{{(?:[\s]+)?(.*)([\s]+)?}} 

i trying string out of following pattern...

{{some_string}} {{    some_string}} {{  some_string   }} {{  some-string      }} {{  string  }} 

additionally, doesn't quite work right there should not trailing spaces on match.

regex101 link: http://regex101.com/r/kt9yt5

for direct match, use \k magic

{{\s*\k[^}]+?(?=\s*}}) 

see demo.

explanation

  • {{\s* matches opening braces , whitespace characters
  • the \k tells engine drop matched far final match returns
  • the negated character class [^}]+? lazily matches characters to...
  • the point lookahead (?=\s*}}) can assert follows spaces , closing braces.

sample code

see output @ bottom of live php demo.

$regex = '~{{\s*\k[^}]+?(?=\s*}})~'; preg_match_all($regex, $yourstring, $matches); print_r($matches[0]); 

output

[0] => some_string [1] => some_string [2] => some_string [3] => some-string [4] => string 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -