7

I'm trying to match a string with Pattern/Matcher, however it is failing because I cannot set Pattern.MULTILINE like the Java documentation uses.

String testString = 'Line 1: Does not match\nCustomer Name: Bob Dole';
String regex = '^\\s*Customer Name\\s*:\\s+(.*)$';

Matcher m = Pattern.compile(regex).matcher(testString);
Boolean hasMatch = m.matches();  //Returns false

According to the Java documentation linked from the Pattern documentation on SFDC this should work:

Matcher m = Pattern.compile(regex, Pattern.MULTILINE).matcher(testString);

However, this gives the error Variable does not exist: Pattern.MULTILINE

Any ideas on how to get multiline working with Pattern?

9

You can use the mode modifier (?m) at the beginning of your regex pattern to specify the multiline option.

String testString = 'Line 1: Does not match\nCustomer Name: Bob Dole';
String regex = '(?m)^\\s*Customer Name\\s*:\\s+(.*)$';

Matcher m = Pattern.compile(regex).matcher(testString);

// iterate through testString finding subsequent matches
while (m.find()) {
    system.debug(m.start() + ': ' + m.group());
}

Debug output:

DEBUG|23: Customer Name: Bob Dole

There are a few other helpful option modifiers that can be used with the regex pattern at the beginning of the string: regular-expressions.info - Using Regular Expressions in Java

|improve this answer|||||
  • 2
    And (?s) equals Pattern.DOTALL!! Thanks for this info, did not know this! – jkraybill May 1 '13 at 0:22
3

Salesforce regular expressions are multiline out of the box. Here's a fragment for your requirement:

String search = Pattern.quote('Customer Name:');
String subject = 'Line 1: Does not match\nCustomer Name: Bob Dole\n123-456-789';

Pattern jsonish = Pattern.compile(
  search      //the search term escaped for regex
  + '\\s+'    //one or more whitespaces
  + '('       //start capturing
  + '[^\\n]+' //one or more of anything other than a newline
  + ')'       //stop capturing
  + '\\n'     //exactly one newline
);
Matcher matcher = jsonish.matcher(subject);
System.assert(matcher.find());                     //true,
System.assertEquals('Bob Dole', matcher.group(1)); //works!
|improve this answer|||||
0

In my experience with using Regex, SF only supports pattern matching in general, not ALL Java options will work with Apex pattern matching. In this particular case, as the error message is telling you, multiline pattern matching is not a feature that's built into the Apex compiler. Instead, you'll need to do a pattern search as part of your regular-expression to find the multi-line symbols.

I recommend you look at string methods and perform string operations first to either split the multi-line strings into single lines before doing your matching, or else simply perform a remove operation on the entire string for the multiline/carriage return symbols. Of course you could include a search for the multi-line symbols/characters as part of your regular-expression too! You have a number of options, its a matter of deciding what will work best for your particular situation.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.