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).


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

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


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

Something like this:

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

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

Leave a Comment