Matching numbers that aren't prices
I am 开发者_Python百科trying to replace all numbers except for prices (numbers starting with $) with X in a body of text. I've been trying to use a look behind to get the job done, but it doesn't seem to work. Here is what I am using now:
$comments = preg_replace("/(?<!$)([0-9]+)/", "x", $comments);
This ends up just replacing all numbers with X including those preceded by $.
You need to escape the dollar sign with a backslash, \$
, otherwise it is interpreted as end of line/string.
Also, the second parentheses set are entirely unnecessary - you are not using the group you capture.
Oh, and to avoid replacing something like $100 you will need to add 0-9 to your negative lookbehind... since you're doing that, you can simply place the dollar inside the character class and escaping is not required.
So at this point we have:
$comments = preg_replace("/(?<![$0-9])[0-9]+/", "x", $comments);
But apparently "preg_replace does not support repition in a look-behind" - which I'm taking to mean you can't put 0-9 in the lookbehind, so instead placing a word boundary before it.
Also something to avoid is replacing $9.99, so hopefully we can specify \d. in the lookbehind to disallow that.
So your code eventually becomes:
$comments = preg_replace("/(?<!\$|\d\.)\b[0-9]+\b/", "x", $comments);
With all this added complexity, you'll want to create some test cases to make sure that works as intended.
$
is a special character in regex (signifying the end of the string). You need to escape it: \$
Also as currently formulated, your lookbehind may only prevent it from replacing the first digit in a price; since after the first digit the lookbehind will proceed to match again since you're no longer at the $.
You might want to use something that includes \b
(word boundaries) to restrict the beginning and end of the matched digit sequence to only full numbers.
This should work also:
/(?<=\s)[0-9]+/
精彩评论