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*}}) explanation
{{\s*matches opening braces , whitespace characters- the
\ktells 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
Post a Comment