12
mFoo = foo;
mBar = bar;
// convert to
this.foo = foo;
this.bar = bar;

How to use a regex to handle this substitution? Please help. Here is the method I used in Android Studio (IntelliJ IDEA) Edit -> Find -> Replace in Path

Text to find: m([A-Z])([A-Za-z0-9]+) = L$1$2
Replace with: this\.L$1$2 = L$1$2

Update

L above is a typo. It should be \L according to JetBrains' document

bitdancer
  • 1,215
  • 2
  • 19
  • 34

3 Answers3

25

You can use a pattern with back-reference and grouping the last words after "=":

Text to find:

m([A-Z])([A-Za-z0-9]+) = (L\1\2)

Replace with:

this.L$1$2 = $3

After your comments, I understand that you have also problems with lowercase/uppercase characters. So try this pattern (I have also simplified the regex):

m(\p{Alpha})(\w+) = (((?i)\1)\2)

and this replace string:

this\.L$1$2 = $3

So with your example with an input text:

mContext = context

you obtain this:

this.LContext = context

I don't know if "L" specified in your text/replace string is your typo error or other but if it's so you can change the "replace string" in the following way:

this\.$3 = $3

So you can obtain this:

this.context = context

Let me know if this help you!

Keale
  • 3,924
  • 3
  • 29
  • 46
Simona R.
  • 558
  • 6
  • 20
  • I'll try the backreference feature on Android Studio and it works – Simona R. Jan 24 '16 at 07:12
  • `L\1` seemed not same as `L$1`, but back-reference `\1` worked. `L\1` cannot match lower case `\1` – bitdancer Jan 24 '16 at 07:16
  • Could you put a text example? So I'll check the mistake – Simona R. Jan 24 '16 at 07:22
  • `mContext = context;` I have checked case sensitive option. – bitdancer Jan 24 '16 at 07:23
  • Ok you have a problem with a lower case not with back-reference. I enriched the answer – Simona R. Jan 24 '16 at 08:02
  • `L` is a typo. It should be `\L` according to [jetbrains document](https://www.jetbrains.com/idea/help/finding-and-replacing-text-in-file.html) `m(\p{Alpha})(\w+) = (((?i)\1)\2)` and `this\.$3 = $3` works. Thanks a lot. – bitdancer Jan 24 '16 at 12:33
  • I tried to use `\L` in **Replace with** text field of IntelliJ IDEA's **Replace in Path** dialog, but it shows some kind of syntax error. I don't know what's going on. – bitdancer Jan 24 '16 at 12:46
2

Thanks to Simona R., a simple example in Android Studio.

I want to replace

parentFragmentManager.let { dialogFragment.show(it, AlertDialogFragment.TAG) } 

to

dialogFragment.show(parentFragmentManager, AlertDialogFragment.TAG) 

in several classes so that two parameters may change.

Then I use replacement with $1 and $2:

parentFragmentManager.let \{ (\w+).show\(it, (\w+).TAG\) \}
$1.show\(parentFragmentManager, $2.TAG\)

enter image description here

CoolMind
  • 26,736
  • 15
  • 188
  • 224
0

Say you have a text file with a list of statements:

This is text one$
This is text two!
Is this text three?

And you want to escape all metacharacter. 1. In Android studio open the "Find and Replace" feature by hitting ctrl+R 2. In the "find" field enter: ([[\\^\$.\|\?*+(){}]) 3. Check the checkbox for "Regex" 4. In the "replace" field enter: \$1

The result should now look like this:

This is text one\$
This is text two\!
Is this text three\?
user3193413
  • 575
  • 7
  • 10