Makers_F
Makers_F

Reputation: 3063

Find/Replace in Eclipse with regex

i need to replace

Sprite name = new Sprite(number1, number2, this.mScritteRegion.get(number3)
{
     stuff_here
};

where name, numberX, and stuff_here are a the part i want to keep

with

RectangularShape name = mLanguageManager.getSpriteWithText(number1, number2, 0, 0, "", getResources.getString(R.string.), mLanguageManager.getFont("main_font"), new IOnAreaTouched()
{
      stuff_here
});

So far i managed with regex to find the first part of the code, but i can't select the new line + { + stuff_here + };

Here the regex

Sprite (\w)* = new Sprite\((\d)*(\s)*,(\s)*(\d)*(\s)*,(\s)*this.mScritteRegion.get\((\d)*\)\)

and what i think i'll need to use to replce it

RectangularShape $1 = mLanguageManager.getSpriteWithText\($2, $5, 0, 0, "", getResources.getString\(R.string.\), mLanguageManager.getFont\("main_font"\), new IOnAreaTouched\(\) $X\)

i put $X because i still don't know what index it will be, in addition i don't know how to handle the }; to });. I must get stuff_here until };, because it is the only delimiter that grants me to catch everything, but i don't know then how to put the parenthesis in between them..

I just started today looking ad regex, so i'm a total noob in this field..

Can you help me?

Upvotes: 1

Views: 409

Answers (1)

ynka
ynka

Reputation: 1497

I did this in NetBeans, I figure the regex must look the same, maybe the substitution characters are different (I will check when I have access to a computer with Eclipse).

The regex to catch your first String is:

(?s)Sprite (\w+) = new Sprite\((\w+), (\w+),.*?(.\{.*?\});

The substitution regex is

RectangularShape $1 = mLanguageManager.getSpriteWithText($2, $3, 0, 0, "", getResources.getString(R.string.), mLanguageManager.getFont("main_font"), new IOnAreaTouched()$4);

A few remarks:

  • the (?s) flag means that the dot (.) can be matched to a new line character
  • there is one assumption here: the }; sequence must not appear in the stuff_here block
  • this transformation loses the new line character at the end of the signature - the semantics are the same though, and you can bring it back with formatting templates.

So, the source being:

Sprite name = new Sprite(number1, number2, this.mScritteRegion.get(number3)
{
  stuff_here
};

I get:

RectangularShape name = mLanguageManager.getSpriteWithText(number1, number2, 0, 0, "", getResources.getString(R.string.), mLanguageManager.getFont("main_font"), new IOnAreaTouched()   {
         stuff_here
    });

Upvotes: 1

Related Questions