Home > front end >  Intellij regex to find and replace
Intellij regex to find and replace

Time:03-31

I need below text to be find by using regex in Intellij

messageSource.getMessage("error1.test1",null, null)
messageSource.getMessage("error2.test2",null, null)
messageSource.getMessage("error3.test3",null, null)
messageSource.getMessage("error4.test4",null, null)
messageSource.getMessage("success",null, null)
messageSource.getMessage("error",null, null)

I need this to replace with below

messageSource.getMessage("error1.test1",null, Locale.ENGLISH)
messageSource.getMessage("error2.test2",null, Locale.ENGLISH)
messageSource.getMessage("error3.test3",null, Locale.ENGLISH)
messageSource.getMessage("error4.test4",null, Locale.ENGLISH)
messageSource.getMessage("success",null, Locale.ENGLISH)
messageSource.getMessage("error",null, Locale.ENGLISH)

Note: These are just some examples, but hundres of lines are there across the project. error1.test1, success these texts can be with . (dot) or without dot , no numerics involved (only alphabets)

I tired to some extend, but couldn't finish exactly messageSource.getMessage\(\"[a-zA-Z]*.[a-zA-Z]*",[null]*,[null]*

CodePudding user response:

You may try the following find and replace, in regex mode:

Find:    messageSource.getMessage\(\s*(\S ?)\s*,\s*null\s*,\s*null\s*\)
Replace: messageSource.getMessage($1, null, Locale.ENGLISH)

CodePudding user response:

Seems like Structural Search & Replace would work well here (Edit | Find | Replace Structurally). Example template for your use case:

<replaceConfiguration name="stackoverflow" text="messageSource.getMessage(&quot;$text$&quot;,null, null)" recursive="false" type="JAVA" pattern_context="default" search_injected="false" reformatAccordingToStyle="false" shortenFQN="false" replacement="messageSource.getMessage(&quot;$text$&quot;,null, java.util.Locale.ENGLISH)" case_sensitive="true">
  <constraint name="__context__" within="" contains="" />
  <constraint name="text" within="" contains="" />
</replaceConfiguration>

Use the Import Template from Clipboard action in the dialog.

  • Related