Regex not matching
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
I am using this regex expression:
String regex = '(\d11|\d5 \d6|\+\d12)';
Pattern compiledPattern = Pattern.compile(regex);
Matcher match = compiledPattern.matcher('Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)');
if (match.matches())
System.debug('MATCH');
String phone = match.group(1);
else
System.debug('MISS');
To extract the phone number from this String:
Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)
When I test the Regex in:
- Java Regular Expression Tester
- regex101
It works fine.
But when I try and use it in Apex code, it does not find a match.
- What am I doing wrong?
- How can I fix it?
apex regular-expressions
add a comment |Â
up vote
1
down vote
favorite
I am using this regex expression:
String regex = '(\d11|\d5 \d6|\+\d12)';
Pattern compiledPattern = Pattern.compile(regex);
Matcher match = compiledPattern.matcher('Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)');
if (match.matches())
System.debug('MATCH');
String phone = match.group(1);
else
System.debug('MISS');
To extract the phone number from this String:
Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)
When I test the Regex in:
- Java Regular Expression Tester
- regex101
It works fine.
But when I try and use it in Apex code, it does not find a match.
- What am I doing wrong?
- How can I fix it?
apex regular-expressions
There is a difference betweenmatches()
andfind()
in apex. Mind adding your apex that isn't working?
â Derek F
17 mins ago
@DerekF code added
â Robs
15 mins ago
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am using this regex expression:
String regex = '(\d11|\d5 \d6|\+\d12)';
Pattern compiledPattern = Pattern.compile(regex);
Matcher match = compiledPattern.matcher('Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)');
if (match.matches())
System.debug('MATCH');
String phone = match.group(1);
else
System.debug('MISS');
To extract the phone number from this String:
Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)
When I test the Regex in:
- Java Regular Expression Tester
- regex101
It works fine.
But when I try and use it in Apex code, it does not find a match.
- What am I doing wrong?
- How can I fix it?
apex regular-expressions
I am using this regex expression:
String regex = '(\d11|\d5 \d6|\+\d12)';
Pattern compiledPattern = Pattern.compile(regex);
Matcher match = compiledPattern.matcher('Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)');
if (match.matches())
System.debug('MATCH');
String phone = match.group(1);
else
System.debug('MISS');
To extract the phone number from this String:
Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)
When I test the Regex in:
- Java Regular Expression Tester
- regex101
It works fine.
But when I try and use it in Apex code, it does not find a match.
- What am I doing wrong?
- How can I fix it?
apex regular-expressions
apex regular-expressions
edited 15 mins ago
asked 24 mins ago
Robs
1,347524
1,347524
There is a difference betweenmatches()
andfind()
in apex. Mind adding your apex that isn't working?
â Derek F
17 mins ago
@DerekF code added
â Robs
15 mins ago
add a comment |Â
There is a difference betweenmatches()
andfind()
in apex. Mind adding your apex that isn't working?
â Derek F
17 mins ago
@DerekF code added
â Robs
15 mins ago
There is a difference between
matches()
and find()
in apex. Mind adding your apex that isn't working?â Derek F
17 mins ago
There is a difference between
matches()
and find()
in apex. Mind adding your apex that isn't working?â Derek F
17 mins ago
@DerekF code added
â Robs
15 mins ago
@DerekF code added
â Robs
15 mins ago
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
The documentation on the Matcher class should shed some light here.
The description for matches()
states the following
Attempts to match the entire region against the pattern.
Meanwhile the description for find()
is
Attempts to find the next subsequence of the input sequence that matches the pattern. This method returns true if a subsequence of the input sequence matches this Matcher object's pattern.
find()
is what you want to use here, since you want to match a portion of the input.
An example for illustration
String regex = '(\d11|\d5 \d6|\+\d12)';
Pattern msgPattern = Pattern.compile(regex);
Matcher m1 = msgPattern.matcher('Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)');
system.debug(m1.matches()); // displays false
system.debug(m1.find()); // displays true
add a comment |Â
up vote
2
down vote
It does work - but only if you use the method that yields a substring match.
String input = 'Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)';
String regex = '(\d11|\d5 \d6|\+\d12)';
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
System.debug('Find: ' + m.find()); // true
System.debug('Group: ' + m.group()); // 07970123123
System.debug('Matches: ' + m.matches()); // false
Matcher.matches()
asks to find a match against the entire input region. Matcher.find()
locates a group within the input region.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
The documentation on the Matcher class should shed some light here.
The description for matches()
states the following
Attempts to match the entire region against the pattern.
Meanwhile the description for find()
is
Attempts to find the next subsequence of the input sequence that matches the pattern. This method returns true if a subsequence of the input sequence matches this Matcher object's pattern.
find()
is what you want to use here, since you want to match a portion of the input.
An example for illustration
String regex = '(\d11|\d5 \d6|\+\d12)';
Pattern msgPattern = Pattern.compile(regex);
Matcher m1 = msgPattern.matcher('Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)');
system.debug(m1.matches()); // displays false
system.debug(m1.find()); // displays true
add a comment |Â
up vote
3
down vote
accepted
The documentation on the Matcher class should shed some light here.
The description for matches()
states the following
Attempts to match the entire region against the pattern.
Meanwhile the description for find()
is
Attempts to find the next subsequence of the input sequence that matches the pattern. This method returns true if a subsequence of the input sequence matches this Matcher object's pattern.
find()
is what you want to use here, since you want to match a portion of the input.
An example for illustration
String regex = '(\d11|\d5 \d6|\+\d12)';
Pattern msgPattern = Pattern.compile(regex);
Matcher m1 = msgPattern.matcher('Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)');
system.debug(m1.matches()); // displays false
system.debug(m1.find()); // displays true
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
The documentation on the Matcher class should shed some light here.
The description for matches()
states the following
Attempts to match the entire region against the pattern.
Meanwhile the description for find()
is
Attempts to find the next subsequence of the input sequence that matches the pattern. This method returns true if a subsequence of the input sequence matches this Matcher object's pattern.
find()
is what you want to use here, since you want to match a portion of the input.
An example for illustration
String regex = '(\d11|\d5 \d6|\+\d12)';
Pattern msgPattern = Pattern.compile(regex);
Matcher m1 = msgPattern.matcher('Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)');
system.debug(m1.matches()); // displays false
system.debug(m1.find()); // displays true
The documentation on the Matcher class should shed some light here.
The description for matches()
states the following
Attempts to match the entire region against the pattern.
Meanwhile the description for find()
is
Attempts to find the next subsequence of the input sequence that matches the pattern. This method returns true if a subsequence of the input sequence matches this Matcher object's pattern.
find()
is what you want to use here, since you want to match a portion of the input.
An example for illustration
String regex = '(\d11|\d5 \d6|\+\d12)';
Pattern msgPattern = Pattern.compile(regex);
Matcher m1 = msgPattern.matcher('Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)');
system.debug(m1.matches()); // displays false
system.debug(m1.find()); // displays true
answered 11 mins ago
Derek F
18.2k31646
18.2k31646
add a comment |Â
add a comment |Â
up vote
2
down vote
It does work - but only if you use the method that yields a substring match.
String input = 'Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)';
String regex = '(\d11|\d5 \d6|\+\d12)';
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
System.debug('Find: ' + m.find()); // true
System.debug('Group: ' + m.group()); // 07970123123
System.debug('Matches: ' + m.matches()); // false
Matcher.matches()
asks to find a match against the entire input region. Matcher.find()
locates a group within the input region.
add a comment |Â
up vote
2
down vote
It does work - but only if you use the method that yields a substring match.
String input = 'Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)';
String regex = '(\d11|\d5 \d6|\+\d12)';
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
System.debug('Find: ' + m.find()); // true
System.debug('Group: ' + m.group()); // 07970123123
System.debug('Matches: ' + m.matches()); // false
Matcher.matches()
asks to find a match against the entire input region. Matcher.find()
locates a group within the input region.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
It does work - but only if you use the method that yields a substring match.
String input = 'Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)';
String regex = '(\d11|\d5 \d6|\+\d12)';
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
System.debug('Find: ' + m.find()); // true
System.debug('Group: ' + m.group()); // 07970123123
System.debug('Matches: ' + m.matches()); // false
Matcher.matches()
asks to find a match against the entire input region. Matcher.find()
locates a group within the input region.
It does work - but only if you use the method that yields a substring match.
String input = 'Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)';
String regex = '(\d11|\d5 \d6|\+\d12)';
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
System.debug('Find: ' + m.find()); // true
System.debug('Group: ' + m.group()); // 07970123123
System.debug('Matches: ' + m.matches()); // false
Matcher.matches()
asks to find a match against the entire input region. Matcher.find()
locates a group within the input region.
answered 12 mins ago
David Reed
22.3k31640
22.3k31640
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f236529%2fregex-not-matching%23new-answer', 'question_page');
);
Post as a guest
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
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
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
There is a difference between
matches()
andfind()
in apex. Mind adding your apex that isn't working?â Derek F
17 mins ago
@DerekF code added
â Robs
15 mins ago