PDA

View Full Version : Regular expression to remove plus signs between quotes


bauhsoj
07-19-2006, 03:16 AM
What regular expression could I use to remove all plus (+) signs only from within quotes in a string?

For instance, how would I strip out the plus signs in the quotes below but not anywhere else in the string?
+this +is "+a +short +string" +to +test

The string should end up like the following:
+this +is "a short string" +to +test

I have wracked my brain and I can't seem to find a clever way to do this with a regular expression.

ns1987
07-19-2006, 07:37 AM
Try:


$string = '+this +is "+a +short +string" +to +test';
$string = stripslashes(preg_replace('@(\".*\+.*\")@Use', "str_replace('+', '', '$1')", $string));

Beagle
07-19-2006, 03:39 PM
s/\"(([^"+]*)[+]([^"+]*))*\"/\"$1$2\"/g

I think, it's untested. try putzing around with the grouping and the backreferences.

bauhsoj
07-27-2006, 04:32 AM
s/\"(([^"+]*)[+]([^"+]*))*\"/\"$1$2\"/g

I think, it's untested. try putzing around with the grouping and the backreferences.

Will this work with PHP? I am not familiar with the placement of "s/" at the beginning of the regex.

fci
07-27-2006, 01:20 PM
the s at the beginning is a common idiom in certain uses of regex (e.g., with sed or vim), you don't need it in this case.