Making regex case-insensitive

I have created some lines of code for an email handler that will scrape my emails for a particular string (STA0123).

Code:

  // Find STA No. (in email body)
  Opportunity Opp = [SELECT id, STA_No__c Opportunity WHERE Id = :matcher.group(0)];
  String staNo = email.plainTextBody;
  Pattern staPattern = Pattern.compile('STA[0-9]{4}');
  Matcher matcherSta = staPattern.matcher(staNo);
  if (matcherSta.find())
    Opp.STA_No__c = matcherSta.group(0);

This works as expected but only with the first part of the string in capitals, STA0123 matches the pattern but sta0123 does not.

Answer

You need to use JAVA-like CASE_INSENSITIVE pattern (?i)

Something like this:

Pattern staPattern = Pattern.compile('(?i)STA[0-9]{4}');

Attribution
Source : Link , Question Author : Deployment Failure , Answer Author : Sergej Utko

Leave a Comment