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);


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);


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 
  • In the nested case what is the expected output ?– Ofir GFeb 14 at 21:23
  • @OfirG: same. Modified the Q– uSeruSherFeb 14 at 21:24
  • the characters you dilling with are only eng letters and '*' '/' ?, or you can't know for sure ?– Ofir GFeb 14 at 22:39
  • Only eng letters and /* and */– uSeruSherFeb 15 at 14:54

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:


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 to try out regexes online, I find it helpful.

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

    running :

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


    this is test

    running :

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


    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);


    this is test asdasd 

    test 3:

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


    this is test
    • Posted in question itself becuase not able to format comments– uSeruSherFeb 14 at 21:41
    • Thanks for helping me on this. After removing all characters between /* and / 1. Test1 / huhu here here to 2. Test2 this is test asdasd 3. Test3 this test– uSeruSherFeb 15 at 14:53
    • I think regex wont help me and i will have to write separte logic for this to iterate thru string– uSeruSherFeb 15 at 14:54

    Your Answer


    By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

    Not the answer you're looking for? Browse other questions tagged or ask your own question.