Java Regex 2 - Duplicate Words

  • + 2 comments

    I was having trouble setting the CASE_INSENSITIVE flag on the pattern. After setting the pattern like so...

    Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
    

    ...the program was still case sensitive. I looked up some resources online. It does not appear to be a bug with the programming language. I even tried using the Pattern.MULTILINE flag, but this flag setter does not seem to work either. I ended up having to incorporate the case insensitivity in the regex string like this:

    String regex = "(\\b(?i)\\w+\\b) (\\b(?i)\\1\\b)( (?i)\\1((?= )|$))*";
    

    (**Where (?i) sets everything following to be case insensitive)

    I am not sure if I was doing something wrong here. It seems like setting that CASE_INSENSITIVE flag should have been enough, but it seemed to have no effect at all.

    I was able to solve all of the test cases. However, this inconvenience continues to baffles me. It seems like no one else was having any issues with the CASE_INSENSITIVE flag.