Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't Pattern match "/*!"?

Tags:

java

regex

I am trying to match something and for the life of me I cannot figure out WHY it does not work as expected.

String: "/*! preserved */"

Pattern:

Pattern.compile("(/[*][!](?:.+?)[*]/)", Pattern.MULTILINE | Pattern.DOTALL | Pattern.CASE_INSENSITIVE)  
- FAIL

Pattern:

Pattern.compile("(/[*](?:.+?)[*]/)", Pattern.MULTILINE | Pattern.DOTALL | Pattern.CASE_INSENSITIVE) 
- PASS

I only want to catch comments that have a ! right after the opening comment string. I even tried reducing the match to just /*!:

Pattern.compile("(/[*][!])", Pattern.MULTILINE | Pattern.DOTALL | Pattern.CASE_INSENSITIVE) 
- FAIL

Pattern.compile("(/[*!]{2})", Pattern.MULTILINE | Pattern.DOTALL | Pattern.CASE_INSENSITIVE)
- FAIL

Pattern.compile("(/[*][!])", Pattern.MULTILINE | Pattern.DOTALL | Pattern.CASE_INSENSITIVE)
- FAIL


Java Version "1.6.0_43"
Java(TM) SE Runtime Environment (build 1.6.0_43-b01)
Java HotSpot(TM) 64-Bit Server VM (build 20.14-b01, mixed mode)

EDIT

This fails, which does not make sense to me since my pattern does not have a start or end with a requirement:

Pattern p = Pattern.compile("(/[*]!.+?[*]/)", Pattern.MULTILINE | Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher("this is a test /*! preserved */ this is a test /*! preserved1 */");
System.out.println("Pattern: " + p);
System.out.println("Group: " + m.group(1));
System.out.println("Found: " + m.find());

Even reducing it to just match /*! fails:

Pattern p = Pattern.compile("(/[*]!)", Pattern.MULTILINE | Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher("this is a test /*! preserved */ this is a test /*! preserved1 */");
System.out.println("Match: " + m.find());
System.out.println("Pattern: " + p);
System.out.println("Group: " + m.group(1));
like image 481
Digitalxero Avatar asked Sep 07 '26 04:09

Digitalxero


1 Answers

    Pattern p = Pattern.compile("(/[*]!.+?[*]/)", Pattern.MULTILINE | Pattern.DOTALL
            | Pattern.CASE_INSENSITIVE);
    System.out.println(p.matcher("/*! preserved */").matches());

output

true
like image 167
Evgeniy Dorofeev Avatar answered Sep 09 '26 18:09

Evgeniy Dorofeev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!