Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use the captured group in the same regex

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

like image 656
bitdancer Avatar asked Jan 24 '16 06:01

bitdancer


People also ask

How do I reference a capture group in regex?

If your regular expression has named capturing groups, then you should use named backreferences to them in the replacement text. The regex (?' name'group) has one group called “name”. You can reference this group with ${name} in the JGsoft applications, Delphi, .

How do you reference a capture group?

Normally, within a pattern, you create a back-reference to the content a capture group previously matched by using a backslash followed by the group number—for instance \1 for Group 1. (The syntax for replacements can vary.)

How do I capture two groups in regex?

They are created by placing the characters to be grouped inside a set of parentheses ( , ) . We can specify as many groups as we wish. Each sub-pattern inside a pair of parentheses will be captured as a group. Capturing groups are numbered by counting their opening parentheses from left to right.

Can I use named capture groups?

Numbers for Named Capturing Groups. Mixing named and numbered capturing groups is not recommended because flavors are inconsistent in how the groups are numbered. If a group doesn't need to have a name, make it non-capturing using the (?:group) syntax.


1 Answers

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

like image 131
CoolMind Avatar answered Sep 22 '22 21:09

CoolMind