I am writing a program where I need to ignore comments in the file passed.
I read about Regex pattern in this concern..and am able to ignore single line comments(//...) and multiple line comments, if its defined in a single line (/.../).
But am facing difficulty in ignoring multiple line comments like shown below:
>
/* ........
..........
....*/
for single line I used
"//.*$"
and for the second one,
"/\ \ * . * \ \ * /"
Somewhere I even read that Reluctanat quantifier would be helpful...tried with differnt patterns using regex and reluctant quantifier...no joy..
Can someone help me with this..?
Thanks
I think it will works for you :
/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/
or for both single line and multiline comment :
(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|(//.*)
The . normally does not match new line characters like \r, \n and others.
You can however use DOT_ALL which changes this behaviour. This is identical with the (?s).
"(?s)/\\*.*\\*/"
"/\\*[.\r\n]*\\*/"
This replacement should be done before treating single line comments. And there . should not match a new line.
Thanks all for the suggestions..However,I got one exact solution... the pattern
"(?s)/\*.+?\*/" works fine in removing multiline comments..
You may use following to solve your problem
"(?s)/\*.\/"
"/\*[.\r\n]\/"
Related
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")
So I have been trying out different ways to represent information in the console and I have noticed printing \b doesn't remove newlines in the console.
Here is an example:
System.out.println("ggg");
System.out.print("\b\b\b\b\b\b\b\b\b\b\b\b");
shows up as ggg.
Is there a way to make this work?
"/b" just omit a character from the console; the problem is that in your code the "ggg" line will be printed and thew following "/b"s will be printed on a new line in the console, they can not affect the prior line.
You need to use "/b" on the same line.
Visit http://www.java2s.com/Code/Python/String/EscapeCodesbtnar.htm to grasp java escape codes.
\b doesn't remove new lines, depending on the OutputStream, it may remove a character. For example, try
System.out.println("ggg\b");
I've ran into a bit of a rough spot in this Java program I'm writing an thought I would ask for some help. I'm using regex to replace certain lines in a file being read in and not getting the desired result. I want to replace all series of 3 new lines in my file and thought this would be straight forward since my regex is working in notepad++ but I guess not. Below is what an example of what the file is like:
FIRST SENTENCECRLF
CRLF
CRLF
CRLF
CRLF
CRLF
SECOND SENTENCECRLF
So, in other words, I am wanting to remove 3 of those carriage return\line feed instances between the first and second sentence lines. Below is what I've tried so far. The first tried in Java results in no change to the file (works in Notepad++ fine). The second, pretty much the same as the first works in notepad++ but not Java. The third is pretty much the exact same case as the other two. Anyone have any helpful suggestions as to what might work in this situation. At this point anything would be greatly appreciated!
^(\r\n){3}
^\r\n(\r\n)(\r\n)
^\r\n\r\n\r\n
Try the following regex:
(?m)^(\r\n){3}
The (?m) enables multi-line mode in Java, as explained in How to use java regex to match a line
I am working on a project that requires me to remove comments from a java file. Currently, I am using the regular expression
(?:/\\*(?:[^*]|(?:\\*+[^*/]))*\\*+/)|(?://.*)
which I got from http://ostermiller.org/findcomment.html.
The regular expression works well, but the problem is that I need to preserve the file structure when I remove the comments. In other words, if I have a 3 line block comment, I need it to be replaced with 3 blank lines. This is necessary so that the code remains on the same line numbers as the original.
How would I replace the 3 line block comment with 3 blank lines?
Edit:
I was able to solve my problem by making use of SableCC.
I haven't fully sussed out what that regex is doing, but if it matches the entire comment, then you can get the matched comment, check to see how many newlines it contains, and then replace the match with that many newlines instead of replacing it with the empty string.
If you're set on regex you can try this
~/(?:/.*?$|\*[^*]*\*/)~
DEMO
This makes use of two different non-capture groups
Since all comments (single-line and multi-line) have to start with a / that's the first character of the regex. Then a comment can have another / or a *. This is where the alternation comes in. The first part /.*?$ handles single line comments, while the second part \*[^*]*\* matches on multi-line comments.
If your multi-line comments are formatted with leading * followed by a <space>, like this:
/* mu
* lti
* line
* comment
*/
then this DEMO should do the trick (I don't think a line can start with a * in Java, unless it's in a comment).
Unfortunately, I have not found a suitable substitution to preserve line spacing if they are not formatted as above.
im new to regexes , I have email validation program with the given conditions for a valid email
# and . should be present only once
there should be five characters between # and .
there should be at least 3 characters before #
# should always precede the .
I cannot figure out the last part. Any help with a little explanation would be great :)
You should just use a standard regex. I generally use
\b[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b.
Have a look at http://www.regular-expressions.info/email.html for different examples.
Validating an email using regex is really a tough one and you'll never get satisfied no matter what you use!
Here is the regex that only based on your four points on the question. Assuming by any character you mean [a-zA-Z0-9]:
(?=^[^.#]*#[^.#]*\.[^.#]*$)[a-zA-Z0-9]{3,}#[a-zA-Z0-9]{5}\.
Online Demo
Since you'll use is in Java, use \\ for every \ in your code.