Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

phpstorm regex search and replace

Tags:

regex

phpstorm

I trying to regex replace echo $review->helpful; with echo stripslashes($review->helpful); in PHPstorm without any luck.

I tried echo \$.*\; with echo stripslashes($1); but didn't worked I get malformed replacement string.

Any help will be appreciated.

Thanks

like image 386
Marcus Avatar asked Jan 14 '13 07:01

Marcus


People also ask

Can I use regex in replace?

The Regex. Replace(String, String, MatchEvaluator, RegexOptions) method is useful for replacing a regular expression match if any of the following conditions is true: If the replacement string cannot readily be specified by a regular expression replacement pattern.

How do you replace a word in regex?

To use RegEx, the first argument of replace will be replaced with regex syntax, for example /regex/ . This syntax serves as a pattern where any parts of the string that match it will be replaced with the new substring. The string 3foobar4 matches the regex /\d. *\d/ , so it is replaced.


1 Answers

I'm not familiar with phpstorm, but the reason you're getting a malformed replacement string error is probably because you're using $1 to reference the first grouping, when there is no first grouping.

Try using this:

echo \$(.*?);

And replace again with this, like you originally did:

echo stripslashes($1);

Basically all I did was group .* so that $1 would be able to reference it, and added a lazy modifier to the star just to avoid any weird stuff happening later on in the parse. I also removed the \, since ; itself doesn't stand for anything in regex, escaping it is unnecessary.

Here's a test to verify that it works: http://fiddle.re/9e47

like image 125
Michael Avatar answered Oct 27 '22 19:10

Michael