Eclipse change case in regex find and replace - java

In Eclipse, I would like to be able to do a regex search and replace for some text and slightly modify it, changing the case of one of the letters. For example: find myVariable.getProperty() and change it to myVariable.property.
I can easily use myVariable.get(\w+)\(\) and replace it with myVariable.$1, but that results in myVariable.Property with the capital 'P'.
I believe this is possible with some regex engines, but I cannot find a way to do it within Eclipse.

I don't think eclipse supports that type of functionality. You would have to get "creative" and do things like:
Search: myVariable\.getP(\w+)\(\)
Replace: myVariable\.p(\1)
But according to regular-expresions.info (http://www.regular-expressions.info/replacecase.html), if you're open to editing your JSP file with a different text editor, there are programs that use other flavors of RegEx which can make your change.
Using your example, EditPad Lite for instance would allow your search:
Search: (myVariable\.)get(\w+)\(\)
And replace it with:
Replace: \1\L2
This would result in:
myVariable.getProperty()
to:
myVariable.property
In this case \L2 changes the contents of the second back reference to a lowercase version. \U2 would change it to uppercase. \I0 would capitalize the initial letter of each separated word in the string and \F0 would capitalize just the first letter of your string.
I've done similar things for small but repetitive changes where eclipse is not exactly equipped for the job. And then go back to eclipse when the change has gone through.

Related

Regex to match conan dependency from conanfile.txt

I am trying to create a regex in Java to match and get the name, version, channel and owner for each dependency but I haven't been able to have one that covers all the possible scenarios:
the structure is something like name/version#owner/channel, where the version might have a semver structure, the owner and channel are optional.
Currently, I have :
^(?<name>[\d\w][\d\w\+\.-]+)\/(?<version>[\d\w][\d\w\.-]+)(#(?<owner>\w+))?(\/(?<channel>.+))?$
but it's failing for boost_atomic/1.59.0+4#owner/release, since the +4 is not matched and I need the value before that -> 1.59.0
Some other scenarios that need to be valid and are valid for the regex above are:
Poco/1.9.0#pocoproject/stable
zlib/1.2.11#conan/stable
freetype/2.10.1/stable
openssl/1.0.2g/stable
openssl/1.0.2g
openssl/1.0.2g#owner
Also, there might be some dependencies with comments :
zlib/1.2.11#conan/stable # comment
In that case I would need to get rid of the component and only get the relevant information with the regex.
I am not sure if my current regex is good, but from what I've tested only some scenarios are missing
You can simplify your regex and avoid putting too many characters in that character set and escaping them, instead use something like [^\/] to capture anything except / as you want to capture anything preceding a slash.
I've made some modifications and the updated regex that should work for you is following,
^(?<name>[^\/]+)\/(?<version>[^\/#\s]+)(#(?<owner>\w+))?(\/(?<channel>\S+))?(?:\s*#\s*(?<comment>.+))?$
I've added another named group for comment as you mentioned that can also be present. Let me know if this works for you.
Try this demo
Edit: If channel contains a text like release:132434 and anything followed by a colon is to be ignored as part of channel, you can use updated regex below,
^(?<name>[^\/]+)\/(?<version>[^\/#\s]+)(?:#(?<owner>\w+))?(?:\/(?<channel>[^:\s]+)\S*)?(?:\s*#\s*(?<comment>.+))?\s*$
Updated Demo

Find words in multiple files starting with a specific set of characters and replace the whole word with another word

I need to read through multiple files and check for all occurrences of words that start with a specific pattern and replace it in all the files with another word. For example, I need to find all words beginning with 'Hello' in a set of files which may contain words like 'Hellotoall' and then I want the word to be replaced with 'Greetings', just an example. I have tried:
content = content.replaceAll("/Hello(\\w)+/g", "Greetings");
This code results in : Greetingstoall, but I want the whole word to be replaced with 'Greetings', i.e. if the file has a line:
Today i say Hellotoall present here. After replacement the line should be like: Today i say Greetings present here.
How can I achieve such a requirement with a better regex.
You need just "Hello(\\w)*".
isn't the output Greetingsoall? The match would be Hellot - so first thing is that you may want to replace + with *
As talex pointed out, there is sed syntax mixed in, which doesn't work with Java.
content.replaceAll("Hello\w*", "Greetings")

Best way to store words for given scenerio

I am working on Java project [Maven].
I am confused in one point. I don't know what is logiclaly corect.
Problem is as follows :-
Sentence is given, and from their I have extract some particular words.
Solution that I found
I make one regex and put in Constants class. Whenever I have to add more words, I simply appended words in regex.
This solves the problem.
I am confused here
I am thinking, if I put numbers of text files in resources folder where each text file denotes one regex expression.
REGEX = (?:A|B|C|D)
A, B, C, D = Word(String)
Is it a good idea ? If not please suggest any other.
Why would you save regex's in a text file? The fact that you're using a regex seems like an implementation detail that you would want to encapsulate (unless you want the significantly greater functionality but also overhead of supporting regexes).
Also, why do you need new files for each word? That seems like you could just have one file with a word per line that is all of the words you're interested in. This would be much more simple for a user to understand than 100 files with one regex per file.
As my understanding, you want to find some key words from the input string. And those key words could be extened according your requirments.
your current solution is to make this regex (?:A|B|C|D) in your Constant class, wheneveer it's required, you'll add more key words in this regex.
If my understanding is not wrong, maybe, one suggestion is to put this regex in your properties file, like this
REGEX = (?:city|Animal|plant|student)
if too long, it's could be like this
REGEX = (?:city|Animal|plant|student|car|computer|clothes|\
furnature|others)
Your second idea, if my understanding is not wrong, is to put the keywords as the file name, and those files are put in one resource folder. therefore, you could obtain those files name to compose the final regexp. If your regex are always fixed as the (?:A|B|C|D) format, then this solution is good & convenient. (Every time, you add one new keyword file, you don't need to modify any source code & property file)

Is there a quick way to refactor the entire codebase to adopt a different code convention?

I have received ownership of a code base that, although very well written, uses a rather bizarre convention:
public void someMethod(String pName, Integer pAge, Context pContext)
{
...
}
I'd like to make the following two changes to the entire code:
public void someMethod(String name, Integer age, Context context) { ... }
Opening bracket in the same line of the method declaration
Use a camelCase name for all parameters of the method, without this weird "p" prefix
Can checkstyle help me here? I'm looking but I can't find a way to rename all parameters in all method signatures to something more pleasant.
If you are willing to use the eclipse IDE it'd offer a very handy feature for auto-formatting code:
http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fpreferences%2Fjava%2Fcodestyle%2Fref-preferences-formatter.htm
It is pretty self-explanatory and straight-forward in my opinion.
Eclipse allows for regex based search and replace operations.
Just open Search > File... there enter the following regex for Containing text:
\b[p]([A-Z][a-z]+)\b
And tick both Case sensitive and Regular expression.
Then press Replace...
In the newly popped up window enter
\1
in the With: field and tick Regular expression.
Edit: Sadly in its current version Eclipse does not support the \L flag for content groups so you are still stuck with an uppercase leading letter.
To answer your question about checkstyle: No, checkstyle is a tool used for analyzing code not for changing.
Using checkstyle to format code
(Question from Oct'12)
Also did some research, here's another stackoverflow question aiming at the practically same. The solution offered there is similarly work intensive.
Can I automatically refactor an entire java project and rename uppercase method parameters to lowercase?
(Question from Oct'10)

Eclipse / Rename parameters in a project

I'd like to change the naming conventions of parameters. Until now we've used a "p"-prefix, but I think it isn't necessary and I would like to get rid of the prefix and also change the uppercase character which follows the "p" to a lowercase. Is this somehow possible to do in the whole project in "one run"?
To change the name of one parameter in the whole project in Eclipse do the following:
Click on the parameter.
press [alt] + [shift] + [R].
enter the new name.
press enter to accept the new name, it will change over the whole project. This also works on class names, methods etc.
I'm sorry, but this way you have to do this for all parameters again.
Another solution is to use File / Search. You can do a find and replace in all the files in the workspace. This accepts regular expressions as well.

Categories