0

I need a regular expression that matches a substring in string /*exa*/mple*/ ,

the matched string must be /*exa*/ not /*exa*/mple*/.

It also must not contain "*/" in it.

I have tried these regex:

  1. "/\\*[.*&&[^*/]]\\*/" ,
  2. "/\\*.*&&(?!^*/$)\\*/"

but im not able to get the exact solution.

nafas
  • 5,283
  • 3
  • 29
  • 57

2 Answers2

0

you can try this:

/\*[^\*\/\*]+\*/   --> anything that is in between (including) "/*" and "*/"

Here is a sample:

    Pattern p = Pattern.compile("/\\*[^\\*\\/\\*]+\\*/");
    Matcher m = p.matcher("/*exa*/mple*/");
    while (m.find()){
        System.out.println(m.group());
    }

OUTPUT:

/*exa*/

nafas
  • 5,283
  • 3
  • 29
  • 57
0

I understand you want to pick out comments from a text.

Pattern p = Pattern.compile("/\\*.*?\\*/");
Matcher m = p.matcher("/*ex*a*/mple*/and/*more*/ther*/");
while (m.find()){
    System.out.println(m.group());
}
laune
  • 31,114
  • 3
  • 29
  • 42