简体   繁体   中英

Using regex to remove text between delimiter in java

Hi I'm trying to remove all code between /* and */ in a string of multiple lines. I have this so far:

Scanner scan = new Scanner(inputFile).useDelimiter("\\Z");
String file = scan.next();
String next = file.replaceAll("((/\\*)(\\s|\\S)*(\\*/))", "");

However when I try to run this with an input file that has multiple cases of /* and */ it removes everything between them. For example, in this input

/* first comment */
a = b;
//test
if a = c;
// whatever
if a = d;
/* this
is a

test
*/

/* another */

It will remove everything between the first /* and the last */ at the end of the file. Essentially replacing the entire text with a blank space. How do I limit it to the first case of /** and **/ ?

You are using greedy quantifier. Your (\\\\s|\\\\S)* will match everything till it finds the last */ , till where results in the complete pattern can be successfully matched.

You can make the quantifier reluctant by adding a ? after * .

"((/\\*)(\\s|\\S)*?(\\*/))"

Also, you can simplify your regex like this: -

String next = file.replaceAll("(?s)/\\*.*?\\*/", "");

(?s) -> Is used for SingleLine matching. It is an alternative for Pattern.DOTALL to be used in String.replaceAll() . So, your dot(.) would include everything including newline. And you don't need (\\\\s|\\\\S) for that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM