About Regexp search

Hello,

I'm a little bit confused with regexp search and replace in phpstorm.

I'd like to replace this kind of text _("any_text") by _('any_text').

I've entered the following pattern : (_\("(.*)"\))

It finds correctly this kind of text, but, in the case i have this text two (or more) times on the same line (eg echo _("first_text") . ' : ' ._("second text") ), the found text by phpstorm is _("first_text") . ' : ' ._("second text"), not only _("first_text") (and after _("second_text") )

The second problem is that i can't find the solution to replace only the double quotes (only in this kind of text) by the single quotes.

We're is my mistake and what is the solution for finding and replacing correctly ?

Thanks by advance

Sylvain

 

2 comments

Hello,

 

After searching, i've found a solution :

Search : _\("(.*?)"\)

Replace with  : _\('$1'\)

The only problem is when there's one (or more) single quote in the the text (as this single quote needs to be escaped).

Is there any solution for that ?

Thanks by advance

Sylvain

 

0

Hi there,

Possible alternative solution (really depends on what you have got there -- will not cover all cases; only simple ones).

  1. Install "PHP Inspections (EA Extended)" plugin
  2. It has quite a few inspections ... one of which is "Code Style | Unnecessary double quote". It looks for PHP strings where double quotes can safely be replaced by single quotes. Limitation: it works in all strings -- you cannot specify "look for underscore function only"
  3. Use "Code | Inspect Code..." (to run all possible inspections) .. or "Code | Run Inspection By Name..." to run specific Inspection only (which would be the best choice here)
  4. Once done, in "Inspection Results" window, you can see all found matches -- you can then apply "Replace with single quotes" fix (via context menu) to selected results/whole file or folder

P.S.

The aforementioned "Replace with single quotes" intention/fix can automatically fix single quotes inside double quoted string ... but actual Inspection will not find such places (as it only does what the Inspection's name says). However, you can manually invoke that fix via Alt+Enter menu if caret is inside of such string. Obviously, it's not really usable if you need to change a lot of stuff/lot of files...

0

Please sign in to leave a comment.