Regex implementation in Salesforce: getting from known string to end of the line
I have an email service that parses an email and inserts or updates a set of fields.
I have trawled through previous questions and can see that many regex posts are voted down for being not 'Salesforce' enough but I have tested this everywhere: https://regex101.com/ and the formula works just not in Salesforce:
The data below is a pretend email I may receive:
Job Title: Project Manager
First Name: Alpha
Last Name: Tester
This may also be:
Job Title: Trawler
First Name: Better
Last Name: Tester
My regex is:
(?<=Job Title: ).*$
Which returns:
Both Trawler and Project Manager when processed at regex101
In Salesforce I get:
Insert failed. First exception on row 0; first error:
REQUIRED_FIELD_MISSING, Required fields are missing: [Job Title]: [Job
Title]
Because it matches nothing in the email.
I found this post: Regex: Make Dot Match Newline? and it was quite helpful and mentions that Salesforce implements a different 'end-of-line' code. When I look at https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html is doesn't seem to make any such suggestion.
Clearly I'm being stupid and just cannot see the answer so:
How can I complete this regex in Apex to make this match 'All characters from the known item to the end of the line, no matter how many words, characters or white space characters'?
edit: adding apex code
Called from here:
myPlainText = email.plainTextBody;
mySubject = email.subject;
newthing.field__c = getItemFromString('Job Title: ', myPlainText);
Separate method:
when 'Job Title: '{
//MyPattern = Pattern.compile('Job Title: (\S+)\s');
//MyPattern = Pattern.compile('Job Title: .*$');
MyPattern = Pattern.compile('(?<=Job Title: ).*$');
MyMatcher = MyPattern.matcher(pEmailBody);
if(MyMatcher.find()){
response = MyMatcher.group(0);
}
}
apex apex-email-service regular-expressions
add a comment |
I have an email service that parses an email and inserts or updates a set of fields.
I have trawled through previous questions and can see that many regex posts are voted down for being not 'Salesforce' enough but I have tested this everywhere: https://regex101.com/ and the formula works just not in Salesforce:
The data below is a pretend email I may receive:
Job Title: Project Manager
First Name: Alpha
Last Name: Tester
This may also be:
Job Title: Trawler
First Name: Better
Last Name: Tester
My regex is:
(?<=Job Title: ).*$
Which returns:
Both Trawler and Project Manager when processed at regex101
In Salesforce I get:
Insert failed. First exception on row 0; first error:
REQUIRED_FIELD_MISSING, Required fields are missing: [Job Title]: [Job
Title]
Because it matches nothing in the email.
I found this post: Regex: Make Dot Match Newline? and it was quite helpful and mentions that Salesforce implements a different 'end-of-line' code. When I look at https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html is doesn't seem to make any such suggestion.
Clearly I'm being stupid and just cannot see the answer so:
How can I complete this regex in Apex to make this match 'All characters from the known item to the end of the line, no matter how many words, characters or white space characters'?
edit: adding apex code
Called from here:
myPlainText = email.plainTextBody;
mySubject = email.subject;
newthing.field__c = getItemFromString('Job Title: ', myPlainText);
Separate method:
when 'Job Title: '{
//MyPattern = Pattern.compile('Job Title: (\S+)\s');
//MyPattern = Pattern.compile('Job Title: .*$');
MyPattern = Pattern.compile('(?<=Job Title: ).*$');
MyMatcher = MyPattern.matcher(pEmailBody);
if(MyMatcher.find()){
response = MyMatcher.group(0);
}
}
apex apex-email-service regular-expressions
1
Can you edit your question to include the portion of your apex that builds/uses the regex? The regex is correct, but you may be getting tripped up onfind()
vsmatches()
– Derek F
Jan 21 at 16:18
@DerekF added code
– SeanGorman
Jan 21 at 16:36
The code you have included is a bit incomplete to understand where you are going awry.
– Adrian Larson♦
Jan 21 at 16:58
add a comment |
I have an email service that parses an email and inserts or updates a set of fields.
I have trawled through previous questions and can see that many regex posts are voted down for being not 'Salesforce' enough but I have tested this everywhere: https://regex101.com/ and the formula works just not in Salesforce:
The data below is a pretend email I may receive:
Job Title: Project Manager
First Name: Alpha
Last Name: Tester
This may also be:
Job Title: Trawler
First Name: Better
Last Name: Tester
My regex is:
(?<=Job Title: ).*$
Which returns:
Both Trawler and Project Manager when processed at regex101
In Salesforce I get:
Insert failed. First exception on row 0; first error:
REQUIRED_FIELD_MISSING, Required fields are missing: [Job Title]: [Job
Title]
Because it matches nothing in the email.
I found this post: Regex: Make Dot Match Newline? and it was quite helpful and mentions that Salesforce implements a different 'end-of-line' code. When I look at https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html is doesn't seem to make any such suggestion.
Clearly I'm being stupid and just cannot see the answer so:
How can I complete this regex in Apex to make this match 'All characters from the known item to the end of the line, no matter how many words, characters or white space characters'?
edit: adding apex code
Called from here:
myPlainText = email.plainTextBody;
mySubject = email.subject;
newthing.field__c = getItemFromString('Job Title: ', myPlainText);
Separate method:
when 'Job Title: '{
//MyPattern = Pattern.compile('Job Title: (\S+)\s');
//MyPattern = Pattern.compile('Job Title: .*$');
MyPattern = Pattern.compile('(?<=Job Title: ).*$');
MyMatcher = MyPattern.matcher(pEmailBody);
if(MyMatcher.find()){
response = MyMatcher.group(0);
}
}
apex apex-email-service regular-expressions
I have an email service that parses an email and inserts or updates a set of fields.
I have trawled through previous questions and can see that many regex posts are voted down for being not 'Salesforce' enough but I have tested this everywhere: https://regex101.com/ and the formula works just not in Salesforce:
The data below is a pretend email I may receive:
Job Title: Project Manager
First Name: Alpha
Last Name: Tester
This may also be:
Job Title: Trawler
First Name: Better
Last Name: Tester
My regex is:
(?<=Job Title: ).*$
Which returns:
Both Trawler and Project Manager when processed at regex101
In Salesforce I get:
Insert failed. First exception on row 0; first error:
REQUIRED_FIELD_MISSING, Required fields are missing: [Job Title]: [Job
Title]
Because it matches nothing in the email.
I found this post: Regex: Make Dot Match Newline? and it was quite helpful and mentions that Salesforce implements a different 'end-of-line' code. When I look at https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html is doesn't seem to make any such suggestion.
Clearly I'm being stupid and just cannot see the answer so:
How can I complete this regex in Apex to make this match 'All characters from the known item to the end of the line, no matter how many words, characters or white space characters'?
edit: adding apex code
Called from here:
myPlainText = email.plainTextBody;
mySubject = email.subject;
newthing.field__c = getItemFromString('Job Title: ', myPlainText);
Separate method:
when 'Job Title: '{
//MyPattern = Pattern.compile('Job Title: (\S+)\s');
//MyPattern = Pattern.compile('Job Title: .*$');
MyPattern = Pattern.compile('(?<=Job Title: ).*$');
MyMatcher = MyPattern.matcher(pEmailBody);
if(MyMatcher.find()){
response = MyMatcher.group(0);
}
}
apex apex-email-service regular-expressions
apex apex-email-service regular-expressions
edited Jan 21 at 17:12
SeanGorman
asked Jan 21 at 16:10
SeanGormanSeanGorman
626518
626518
1
Can you edit your question to include the portion of your apex that builds/uses the regex? The regex is correct, but you may be getting tripped up onfind()
vsmatches()
– Derek F
Jan 21 at 16:18
@DerekF added code
– SeanGorman
Jan 21 at 16:36
The code you have included is a bit incomplete to understand where you are going awry.
– Adrian Larson♦
Jan 21 at 16:58
add a comment |
1
Can you edit your question to include the portion of your apex that builds/uses the regex? The regex is correct, but you may be getting tripped up onfind()
vsmatches()
– Derek F
Jan 21 at 16:18
@DerekF added code
– SeanGorman
Jan 21 at 16:36
The code you have included is a bit incomplete to understand where you are going awry.
– Adrian Larson♦
Jan 21 at 16:58
1
1
Can you edit your question to include the portion of your apex that builds/uses the regex? The regex is correct, but you may be getting tripped up on
find()
vs matches()
– Derek F
Jan 21 at 16:18
Can you edit your question to include the portion of your apex that builds/uses the regex? The regex is correct, but you may be getting tripped up on
find()
vs matches()
– Derek F
Jan 21 at 16:18
@DerekF added code
– SeanGorman
Jan 21 at 16:36
@DerekF added code
– SeanGorman
Jan 21 at 16:36
The code you have included is a bit incomplete to understand where you are going awry.
– Adrian Larson♦
Jan 21 at 16:58
The code you have included is a bit incomplete to understand where you are going awry.
– Adrian Larson♦
Jan 21 at 16:58
add a comment |
1 Answer
1
active
oldest
votes
You need to turn on multi-line mode in order for the $
specifier to match at a line terminator (rather than at the end of the string). Multi-line mode is activated with the token (?m)
.
As Derek F. alluded to in a comment, you'll also need to use the find()
method (rather than matches()
) in order to identify a substring match.
Here's a sample using your regex, with this modification, that debugs "Project Manager":
String test = 'Job Title: Project ManagernFirst Name: AlphanLast Name: Tester';
Pattern regex = Pattern.compile('(?m)(?<=Job Title: ).*$');
Matcher regexMatcher = regex.matcher(test);
while (regexMatcher.find() == true) {
System.debug(regexMatcher.group());
}
I added my code above.
– SeanGorman
Jan 21 at 16:42
This did it. I had to remove a line I was using to make up for the fact I couldn't find a way to remove the key. The (?m)(?<=key) really helped. thank you
– SeanGorman
Jan 21 at 17:04
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "459"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f247405%2fregex-implementation-in-salesforce-getting-from-known-string-to-end-of-the-line%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to turn on multi-line mode in order for the $
specifier to match at a line terminator (rather than at the end of the string). Multi-line mode is activated with the token (?m)
.
As Derek F. alluded to in a comment, you'll also need to use the find()
method (rather than matches()
) in order to identify a substring match.
Here's a sample using your regex, with this modification, that debugs "Project Manager":
String test = 'Job Title: Project ManagernFirst Name: AlphanLast Name: Tester';
Pattern regex = Pattern.compile('(?m)(?<=Job Title: ).*$');
Matcher regexMatcher = regex.matcher(test);
while (regexMatcher.find() == true) {
System.debug(regexMatcher.group());
}
I added my code above.
– SeanGorman
Jan 21 at 16:42
This did it. I had to remove a line I was using to make up for the fact I couldn't find a way to remove the key. The (?m)(?<=key) really helped. thank you
– SeanGorman
Jan 21 at 17:04
add a comment |
You need to turn on multi-line mode in order for the $
specifier to match at a line terminator (rather than at the end of the string). Multi-line mode is activated with the token (?m)
.
As Derek F. alluded to in a comment, you'll also need to use the find()
method (rather than matches()
) in order to identify a substring match.
Here's a sample using your regex, with this modification, that debugs "Project Manager":
String test = 'Job Title: Project ManagernFirst Name: AlphanLast Name: Tester';
Pattern regex = Pattern.compile('(?m)(?<=Job Title: ).*$');
Matcher regexMatcher = regex.matcher(test);
while (regexMatcher.find() == true) {
System.debug(regexMatcher.group());
}
I added my code above.
– SeanGorman
Jan 21 at 16:42
This did it. I had to remove a line I was using to make up for the fact I couldn't find a way to remove the key. The (?m)(?<=key) really helped. thank you
– SeanGorman
Jan 21 at 17:04
add a comment |
You need to turn on multi-line mode in order for the $
specifier to match at a line terminator (rather than at the end of the string). Multi-line mode is activated with the token (?m)
.
As Derek F. alluded to in a comment, you'll also need to use the find()
method (rather than matches()
) in order to identify a substring match.
Here's a sample using your regex, with this modification, that debugs "Project Manager":
String test = 'Job Title: Project ManagernFirst Name: AlphanLast Name: Tester';
Pattern regex = Pattern.compile('(?m)(?<=Job Title: ).*$');
Matcher regexMatcher = regex.matcher(test);
while (regexMatcher.find() == true) {
System.debug(regexMatcher.group());
}
You need to turn on multi-line mode in order for the $
specifier to match at a line terminator (rather than at the end of the string). Multi-line mode is activated with the token (?m)
.
As Derek F. alluded to in a comment, you'll also need to use the find()
method (rather than matches()
) in order to identify a substring match.
Here's a sample using your regex, with this modification, that debugs "Project Manager":
String test = 'Job Title: Project ManagernFirst Name: AlphanLast Name: Tester';
Pattern regex = Pattern.compile('(?m)(?<=Job Title: ).*$');
Matcher regexMatcher = regex.matcher(test);
while (regexMatcher.find() == true) {
System.debug(regexMatcher.group());
}
answered Jan 21 at 16:23
David ReedDavid Reed
35.4k72053
35.4k72053
I added my code above.
– SeanGorman
Jan 21 at 16:42
This did it. I had to remove a line I was using to make up for the fact I couldn't find a way to remove the key. The (?m)(?<=key) really helped. thank you
– SeanGorman
Jan 21 at 17:04
add a comment |
I added my code above.
– SeanGorman
Jan 21 at 16:42
This did it. I had to remove a line I was using to make up for the fact I couldn't find a way to remove the key. The (?m)(?<=key) really helped. thank you
– SeanGorman
Jan 21 at 17:04
I added my code above.
– SeanGorman
Jan 21 at 16:42
I added my code above.
– SeanGorman
Jan 21 at 16:42
This did it. I had to remove a line I was using to make up for the fact I couldn't find a way to remove the key. The (?m)(?<=key) really helped. thank you
– SeanGorman
Jan 21 at 17:04
This did it. I had to remove a line I was using to make up for the fact I couldn't find a way to remove the key. The (?m)(?<=key) really helped. thank you
– SeanGorman
Jan 21 at 17:04
add a comment |
Thanks for contributing an answer to Salesforce Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f247405%2fregex-implementation-in-salesforce-getting-from-known-string-to-end-of-the-line%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Can you edit your question to include the portion of your apex that builds/uses the regex? The regex is correct, but you may be getting tripped up on
find()
vsmatches()
– Derek F
Jan 21 at 16:18
@DerekF added code
– SeanGorman
Jan 21 at 16:36
The code you have included is a bit incomplete to understand where you are going awry.
– Adrian Larson♦
Jan 21 at 16:58