views:

1325

answers:

8

I'm having trouble coming up with the correct regex string to remove a sequence of multiple ? characters. I want to replace more than one sequential ? with a single ?, but which characters to escape...is escaping me.

Example input:

Is this thing on??? or what???

Desired output:

Is this thing on? or what?

I'm using preg_replace() in PHP.

+8  A: 
preg_replace('{\?+}', '?', 'Is this thing on??? or what???');

That is, you only have to escape the question mark, the plus in "\?+" means that we're replacing every instance with one or more characters, though I suspect "\?{2,}" might be even better and more efficient (replacing every instance with two or more question mark characters.

Nouveau
+2  A: 

preg_replace( '{\\?+}', '?', $text );

should do it.

You need to escape the question mark itself with a backslash, and then escape the backslash itself with another backslash.

It's situations like this where C#'s verbatim strings are nice.

Rob
You don't need to escape the backslash in a single quoted string. You can if you want, but you don't have to.
yjerem
+1  A: 

This should work (I have tested it):

preg_replace('/\?+/', '?', $subject);
yjerem
+1  A: 
preg_replace('/\?{2,}/','?',$text)
MizardX
+1  A: 

this should do it

preg_replace('/(\?+)/m', '?', 'what is going in here????');

the question mark needs to be escaped and the m is for multiline mode.

This was a good web site to try it out at http://regex.larsolavtorvik.com/

Xian
A: 

Geez, five different regexes and they all seem to work. I hate when there's so many different ways to do something :)

davr
Ahh yes... one of the pitfalls (beautiful things??) about reg ex :)
Xian
A: 

have you tried the pattern

[\?]+

with the replacement of "?"

David Lay
A: 
str_replace('??', '?', 'Replace ??? in this text');
powtac
The output of this will be "Replace ?? in this text", unless you put it in a while-loop, and if you do that it'll suddenly be a whole lot less efficient than preg_replace. On my test server, running this, in a loop, 1m times, took (roughly) 2.7 seconds (roughly), whereas the preg_replace only took 1.2 seconds.
grimman