I have the string page-24 and want to replace 24 by any other number.
The script is written in PHP and uses preg_replace, however this problem should be independent from the programming language.
My match pattern is: (.*-)(\d*)
The replace pattern would be: $1[insert number here]$2
The backreferences work, but I cannot insert a number into the given place in the replace pattern because the expression would be interpreted in a wrong way. (First backreference to 199th match, if I inserted 99 as number.)
Am I missing an escape character here?
In PHP, the replacement string either looks like this:
Or like this (if it is in a double-quoted string):
Example:
(You probably just want
${1}99, which would give'page-99'rather than'page-9924'.)See here for documentation in PHP. Perl uses the same notation. Javascript, as far as I can tell, doesn’t support the
${1}notation, but it will interpret$199as${199}if there are at least 199 matching parenthesis, as${19}9if there are at least 19 matching parens, and${1}99if there is at least 1 matching paren, or the literal string$199if there are no matches. I don’t know about other languages’ implementations.