1

I am attempting to do some find and replace on a java source file.

Currently my classes have invalid names (imported from a tool that did poor auto naming) of the form:

public class [0-9]{2}[A-Za-z]+

I would like to insert underscores around the digits, resulting in a valid class name of the form

public class [_][0-9]{2}[_][A-Za-z]+

However using Eclipses find and replace tool, with the regex box check on both the find and replace strings does not format the output as I'd like.

It takes

02ListOfValidAppIDs

and makes it

[-][0-9]{2}[_][A-Za-z]+

instead of

_02_ListOfValidAppIDs

How can you make the regex keep the arbitrary number and text and just plug them in for the replace string?

(Edit: As a note, with the preview feature I can see that eclipse is correctly finding all of the names I wish to replace, and nothing else)

4
  • What are you using in your Replace field? It sounds like you need to create some capture groups so that you can back-reference them in the replace. Commented Jun 1, 2012 at 18:14
  • unfortunately I am not on my machine but one at my internship and am stuck on WinXP. Is there sed-like download for windows? If there is, I am not too familiar with sed beyond that linux has it. Commented Jun 1, 2012 at 18:15
  • @TLS public class [][0-9]{2}[][A-Za-z]+ is my replace field. How would I use capture groups? (for some reason [ _ ] gets formatted weird) Commented Jun 1, 2012 at 18:16
  • @RossLarson you can use cygwin for windows to use sed Commented Jun 1, 2012 at 18:20

2 Answers 2

3

I'm not sure of the exact flavor of Regex that you'll need, but something like this should get you started in the right direction.

Update your "Find" pattern to use capture groups:

(public class )([0-9]{2})([A-Za-z]+)

And then reference those captures in the replacement:

\1_\2_\3

NOTE: Some flavors of Regex will use {} instead of () to represent a captured group, and some flavors will use $1, $2, $3, etc. as the reference instead of using \#.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. For eclipse (in case anyone stumbles across this) it was parens and $#
2

In Eclipse's Find/Replace dialogue, this works fine if you use

([0-9]+)([A-Za-z]+)

in the Find box and

_$1_$2

in the Replace with box.

Of course, the Regular expressions box must be checked too.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.