function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
msnellermsneller 

Regular Expression and Matcher issues

Does anyone know why this will not find the match.

I'm running this code

	public string cleanEmail(String body){
		String result;
		system.debug(body);
		String RegExp = '(?m).*^From:.*\\n^Sent:.*\\n^To:.*\\n^Subject:.*'; 
		
		Matcher m = Pattern.compile(RegExp).matcher(body);
		if (m.matches()) {
			Integer position = m.start();
			result = body.substring(0, position);
		}else{
			result = 'No match';
		}
		return result;
	}

 

The string "body" equals the following and should find a match.

 

email Test 6

 

From: xxxxx@xxxxxx.com
Sent: Wednesday, December 11, 2013 7:07 AM
To: 'xxxxx@xxxxxx.com'
Subject: FW: Case email testing

 

Junk

Best Answer chosen by Admin (Salesforce Developers) 
msnellermsneller

I abandoned the matcher. Here is my solution

	public string cleanEmail(String body){
		String result;
		String RegExp = '(?m).*^From:.*\\n^Sent:.*\\n^To:.*\\n^Subject:.*'; 
		List<String> clean = body.split(RegExp);
		
		if (clean.size() > 0) {
			result = clean.get(0);
		}else{
			result = 'No match';
		}
		return result;
	}