user7392562
user7392562

Reputation:

Regular expression to remove a substring between two strings

There are similar examples around, but I am not able to find my solution

Objective: Remove all (including) the data between /* and */. I am using below code

String str="this /* asdasda */ is test";
str = str.replaceAll("/\\*.*?\\*/","") ;
System.out.println(str);

Output:

this  is test

Problem comes when these are nested

String str="this /* asdas/* extra comments */da */ is test";
str = str.replaceAll("/\\*.*?\\*/","") ;
System.out.println(str);

Output

this da */ is test

I am not able to solve the nested part. I need it to discard anything between /* and */

For example

"this /* asdas/* extra commen/*   asdas  */  ts */da */ is test /* asdas */ asdasd ";

should transalate as

this  is test  asdasd 

Upvotes: 2

Views: 1868

Answers (2)

Ofir G
Ofir G

Reputation: 754

change the regex in replaceAll to /\\*.*\\*/

running :

String str="this /* asdas/* extra comments */da */ is test";
str = str.replaceAll("/\\*.*\\*/","") ;
System.out.println(str);

output:

this  is test

running :

String str="hello /*/* asdas/* eer/* hemmllo*/ */da */ */world";
str = str.replaceAll("/\\*.*\\*/","") ;
System.out.println(str);

output:

hello world

EDIT :

assuming you work with simple words, try this :

replaceAll("(/\\*( *(\\w) *)*)|(( *(\\w) *)*\\*/)","").replaceAll("\\*/|/\\*","");

test 1 :

String str="/* huhu /* jij */ /* */ here /* asfas /* kjk */ kh*/ here to";
str = str.replaceAll("(/\\*( *(\\w) *)*)|(( *(\\w) *)*\\*/)","").replaceAll("\\*/|/\\*","");
System.out.println(str);

output :

   here  here to

test 2:

String str1="this /* asdas/* extra commen/*   asdas  */  ts */da */ is test /* asdas */ asdasd ";
str1 = str1.replaceAll("(/\\*( *(\\w) *)*)|(( *(\\w) *)*\\*/)","").replaceAll("\\*/|/\\*","") ;
System.out.println(str1);

output:

this  is test  asdasd 

test 3:

String str2="this /* /* i */ is /**/ */ test";
str2 = str2.replaceAll("(/\\*( *(\\w) *)*)|(( *(\\w) *)*\\*/)","").replaceAll("\\*/|/\\*","") ;
System.out.println(str2);

output:

this   is   test

Upvotes: 2

geco17
geco17

Reputation: 5294

Java doesn't support recursive matching so you won't be able to get it done all in one go. For languages that do support recursive regex matching, you can try something like:

(\/\*(?>[^\/\*\*\/]+|(?1))*\*\/)

To emulate recursive matching in Java you can add logic outside the actual regex (do several passes).

Taking out the |(?1) or part, you have

(\/\*(?>[^\/\*\*\/]+)*\*\/)

If you use a pattern matcher like so:

    String regex = "(\\/\\*(?>[^\\/\\*\\*\\/]+)*\\*\\/)";
    String str="this /* asdas/* extra commen/*   asdas  */  ts */da */ is test /* asdas */ asdasd ";;

    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(str);
    while (m.find()) {
        str = m.replaceFirst("");
        m = p.matcher(str);
        System.out.println(str);
    }

    // here your string is 
    // this  is test  asdasd 

The last pass will give you the string you're looking for

See https://regex101.com/ to try out regexes online, I find it helpful.

Upvotes: 0

Related Questions