-3

I have a private method that I use for finding drug name using RegEx. The code is as following,

private boolean containsExactDrugName(String testString, String drugName) {

    int begin = -1;
    int end = -1;

    Matcher m = Pattern.compile("\\b(?:" + drugName + ")\\b|\\S+", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE).matcher(testString);
    ArrayList<String> results = new ArrayList<>();

    while (m.find()) {
        results.add(m.group());
    }

    boolean found = results.contains(drugName);
    return found;
}

It should take a drug name and finds exact match inside the text String. That means if the drug name is insuline and the the String text is The patient is taking insulineee for the treatment of diabetes, it will break. It will need the exact match of The patient is taking insuline for the treatment of diabetes.

However, I also need case insensitive matches and if the text is The patient is taking Insuline for the treatment of diabetes or The patient is taking INSULINE for the treatment of diabetes, the method should return true as well.

I put the Pattern.CASE_INSENSITIVE inside the code, however, it doesn't work. How to write it properly ?

Chaklader
  • 159
  • 1
  • 2
  • 16

1 Answers1

5

@Chaklader

Pattern.CASE_INSENSITIVE is the method I'm known to. It should work. for ASCII only case-insensitive matching

Pattern p = Pattern.compile("YOUR_REGEX GOES HERE", Pattern.CASE_INSENSITIVE);

or for Unicode case-folding matching

Pattern p = Pattern.compile("YOUR_REGEX GOES HERE", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);