noi.m
noi.m

Reputation: 3132

php regex from java regex

I'm trying to figure out a regex (in PHP) to find

<ANYTHING_BUT_WHITSPACE>? OR ?<ANYTHING_BUT_WHITSPACE>

and replace the ? with a blank space. So, '?test test?' should become 'test test'. I have one working in java

"(?<=\\S)\\" + "?" + "|\\" + "?" + "(?=\\S)"

Any idea what it would be in PHP?

Upvotes: 0

Views: 64

Answers (1)

AbraCadaver
AbraCadaver

Reputation: 78994

There is probably a better pattern but almost the same:

echo preg_replace('/(?<=\S)\?|\?(?=\S)/', '$1', '?test test?');
  • Need delimiters in this case I used /
  • Don't concatenate with + use . but not needed
  • No need to double escape \\

Upvotes: 1

Related Questions