Regular expression to find System.out.print - java

I try to create one regular expression allows find all System.out.println or print inside all my java classes.
I need to delete all outputs.
Thanks.

"System\\.out\\.println"
use this regex and replace all with empty string.

Not exactly what you asked for but if you are using eclipse to do your development then this would be your best approach.
If you want to search in files: Ctrl+H and then choose tab File Search. Enter your search parameter and it shows all the files where sysout is used. Hope it helps...

Related

Java Regular Expression for "mysql_free_result($myvar);" - Netbeans

I'm using netbeans and I'm working on a project in php.
I need to use the find tool, looking for all the point in the project where the following string can appear
mysql_free_result($myVarName);
$myVarName can be any name allowed in php.
Then I've to replace this string with
mysql_free_result($myVarName); MyAdditionalString
To observe that $myVarName can be different for diffents points in the scripts, so the replace had to replace with correct var name.
Netbeans find tool can use a regular expression for search pattern, so what I've to write to find this points?
Search with
mysql_free_result\(\$[a-zA-Z]+\);
and replace with
$0 myAdditionalString
Thanks to #Jakumi for the suggestion

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)

Java regular Expression to replace System.out.println patterns

We are developing an eclipse plugin tool to remove sysout statements from the workspace projects. We are able to achieve our goal only partially. If the sysouts are in one line we are able to delete it easily. But if the sysout is spanned over a couple of lines (generally occurs due to code formatting), this is when we face the issue.
For Example :
System.out.println("Hello World");
The regular expression to remove this line would be simple:
System.out.println*
But if the code is this:
System.out.println(New Line)("HelloWorld");
This is where the issue comes. Can anyone please suggest how I can replace this using a java regular expression.
I suggest
String regex = "System\.out\.println[^)]+[)]\s*;"
Where the [^)]+ will scan until the closing parenthesis. However, this will fail in multiple cases:
(possibly-unbalanced) parenthesis inside the output
commented-out code
the few cases where it is possible to omit the ';'
cases where System.out is assigned to another variable, instead of being used directly
Go the extra mile and use a Eclipse's in-built parser (which understands lexical issues, comments, and can flag any compile-time references to System.out).

Java Regex for Finding a Pattern and Getting Value in It?

I am working on a plugin. I will parse HTML files. I have a naming convention like that:
<!--$include="a.html" -->
or
<!--$include="a.html"-->
is similar
According to this pattern(similar to server side includes) I want to search an HTML file.
Question is that:
Find that pattern and get value (a.html at my example, it is variable)
It should be like:
while(!notFinishedWholeFile){
fileName = findPatternFunc(htmlFile)
replaceFunc(fileName,something)
}
PS: Using regex at Java or implementing it different(as like using .indexOf()) I don't know which one is better. If regex is good at this situation by performence I want to use it.
Any ideas?
You mean like this?
<!--\$include=\"(?<htmlName>[a-z-_]*).html\"\s?-->
Read a file into a string then
str = str.replaceAll("(?<=<!--\\$include=\")[^\"]+(?=\" ?-->)", something);
will replace the filenames with the string something, then the string can be written back to the file.
(Note: this replaces any text inside the double quotes, not just valid filenames.)
If you want only want to replace filenames with the html extension, swap the [^\"]+ for [^.]+.html.
Using regex for this task is fine performance wise, but see e.g.
How to use regular expressions to parse HTML in Java? and Java Regex performance etc.
I have used that pattern:
"<!--\\$include=\"(.+)(.)(html|htm)\"-->"

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