I'm still developing my regex skills, 开发者_如何学Cso I'm leaning on the community. I say I want to refactor code "with Eclipse", but I've used a number of IDE's whose search and replace functions accept regular expressions. I've successfully create general expressions to find things, but wonder if I can take portions of the matched pattern and use in the replacement value. For example, I have a lot of test functions named with the following pattern" "testSomeFunction1(), testSomeFunction2(), testAnotherFunction()" I'd really like them to be named "test_someFunction1(), test_someFunction2(), test_anotherFunction()". The Find: is "test[A-Z]", but what do I use for Replace with:? "test_[a-z]" is literally replacing? Perhaps, I cannot use a regex statement in the replacement?
For the sample text you've posted, the find expression should be test([a-z]*)
and the replace should be test_$1
.
This makes use of captured groups referenced by $i
where i
is the captured group index (0
is the entire expression). You may also want to consider the case of the search string since text like permuteString
will also match the expression if the search is case-insensitive.
You should also be able to use content-assist in the text fields of the Find/Replace
dialog to see what options are available for regular expressions (once you've checked the box for Regular expressions
) - press CTRL+SPACE
Be sure to check out the \C regular expression operator, which I think is specific to Eclipse. It saves a lot of work in replacing the same word in upper-case, lower-case, and camelCase variants. For example, if the original text is:
SomeObject someObject = SOMEOBJECT;
then doing a "Replace All" replacing
someObject
with
\CanotherObject
will get you:
AnotherObject anotherObject = ANOTHEROBJECT;
which is probably what you want.
精彩评论