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
DevidDevid 

Parse string in custom object fields

string input = 'Lead Informations
Program1: Installation
Lead #: 7894512
Name: Salesforce Dev
Email: test@gmail.com
Phone: (000) 123-7897';

//Marker for final ring duraion
Integer intIndex = input.lastindexOf('Lead Information');

system.debug('### intIndex'+intIndex);
//Remove all characters before intIndex
String newString = input.right(input.length()-intIndex);

system.debug('### newString'+newString);
system.debug('### newString ENd');


// split whole input into lines
string[] lines = newString.split('[\n]+');

string regex = '([^:]+):([^:]+)';

Pattern MyPattern = Pattern.compile(regex);

// Apply regex to every line
for(string inputLine : lines){
	matcher myMatcher = myPattern.matcher(inputLine.trim());
	if(myMatcher.matches() && myMatcher.hitEnd() && myMatcher.groupCount() == 2){
		system.debug('**** Match :'+myMatcher.group(1).trim() + '=>'+myMatcher.group(2).trim());
	}
	else{
		system.debug('No match: line :'+ inputLine);
	}
}


String can be like this:
Example 1:
Lead Informations
Program1: Installation
Lead #: 7894512
Name: Salesforce Dev
Email: test@gmail.com
Phone: (000) 123-7897

Example 2:
Lead Informations
Program1:
Installation
Lead #:
7894512
Name:
Salesforce Dev
Email:
test@gmail.com
Phone: (000) 123-7897

Example 3:
Lead Informations
Program1:

Installation

Lead #:

7894512

Name:

Salesforce Dev

Email:

test@gmail.com

Phone: (000) 123-7897

 

I want to store all Lead #, Name, Email and Phone values to object fields But got stuck how to parse these strings mention above. above code is working for single line break but it does work for multiple line breaks.

David Holland 36David Holland 36
If you know the "labels" (i.e. "Email:", "Name") etc and all is going to be in the same order everytime, you could always hardcode to get all the text between "Name" and "Email", then just do some regex to replace all line breaks and trim additional spaces
 
DevidDevid

Hi, David
Thank for help. I was thinking the same but labels Email, Name and others will not be in the same order. and one more thing I want to point out that we will have this piece of information in a big string  

Example : 
========================================================================================================
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Lead Informations
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Lead Informations
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Lead Informations
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Lead InformationsLorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Lead Informations
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Lead Informations
Program1: Installation
Lead #: 7894512
Name: Salesforce Dev
Email: test@gmail.com
Phone: (000) 123-7897

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

 

=============================================================================================================
So I also want to know that is there any way to parse this big string from bottom to top. So it will be easy to find lead information.