I'm trying to replace -
character with _
inside a double curly brackets.
Example inputs:
Hello {{first-name}}, how are you?
The event {{event-name-address}}
Example outputs:
Hello {{first_name}}, how are you?
The event {{event_name_address}}
This is the regex I tried to do: {{. (-). }}
, and this is the preg_replace
PHP function I tried to use: $template = preg_replace("{{. (-). }}", "$1_", $template);
This doesn't seems to work.
What am I doing wrong? Thanks!
CodePudding user response:
Curve brackets should be escaped, they are regex metasymbols. Than, use two groups for parts before and after the -
:
(\{\{[^}] )-([^}] \}\})
and replace on ${1}_$2
.
[^}]
is used to say "any symbol, but not }
".
UPD: thank to @CarySwoveland for help with clarifying.
CodePudding user response:
If double-braces are matched and not nested you can substitute matches of
(?<=[^{])\-(?=[^{}] }})
with '_'. This assumes the hyphen between matched pairs of double-braces must be preceded by a character other than '{'
and must be followed by a character other than '}'
.1
Note this permits matching multiple hyphens with the the same matched pair of double-braces.
This relies on the assumption that if a hyphen is followed, after intervening characters that are neither open nor closed braces, by a pair of closing double-braces, that hyphen must be preceded by a pair of open double-braces, with intervening characters that are neither open nor closed braces.
The regular expression can be broken down as follow:
(?<= # begin a positive lookbehind
[^{] # match a character other than '{'
) # end positive lookbehind
\- # match '-'
(?= # begin a positive lookahead
[^{}] # match one or more characters other than '{' and '}'
}} # match '}}'
) # end positive lookahead
1. If this is not a requirement the expression can be simplified to \-(?=[^{}]*}})
.
CodePudding user response:
You can use
preg_replace('~(?:\G(?!^)|{{)[^{}-]*\K-(?=[^{}]*}})~', '_', $template)
See the regex demo. Details:
(?:\G(?!^)|{{)
- either the end of the previous match or{{
[^{}-]*
- zero or more chars other than{
,}
and a-
char\K
- match reset operator that omits the text matched so far from the memory buffer-
- a hyphen(?=[^{}]*}})
- a location immediately followed by zero or more chars other than{
and}
and then}}
.