I am trying to extract text between curly braces in PHP. eg
Welcome {$user.first_name} to the {$site} version 1.5. Your username is {$user.username}. Your reputation at present is {$user.reputation.name}
I have used \\{\\$(.*?)\\}
which works fine in some cases. This matches:
But now I want to match only text which has single or multiple . (dot) within the braces. ie For the above string I should be able to match only:
Please help me to achieve this! Thanks in Advance.
You can use
\{\$([^.{}]+(?:\.[^.{}]+)+)\}
See regex demo
Note that instead of lazy dot matching .*?
, I am using a negated character class [^.{}]
that matches any character other than .
, {
or }
. Thus, we match any block that does not contain a .
, and is still within the braces.
The regex breakdown:
\\{\\$
- literal {$
([^.{}]+(?:\\.[^.{}]+)+)
- Group 1 matching
[^.{}]+
- 1 or more characters other than .
, {
, or }
(?:\\.[^.{}]+)+
- 1 or more (due to +
) sequences of
\\.
- a literal dot [^.{}]+
- 1 or more characters other than .
, {
, or }
\\}
- literal }
$re = '/\{\$([^.{}]+(?:\.[^.{}]+)+)\}/';
$str = "Welcome {\$user.first_name} to the {\$site} version 1.5. Your username is {\$user.username}. Your reputation at present is {\$user.reputation.name}";
preg_match_all($re, $str, $matches);
print_r($matches[1]);
you can use this pattern:
~{\$([^}.]*+[^}]+)}~
[^}.]*+
has a possessive quantifier and can't give characters back, so [^}]+
can only match a dot as first character.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.