1

I am trying to parse the string to get the Record name which is auto number D-{0000} format (ex: D-345677) and record id which is 15 to 18 char length which prefix starts with(a7s7) and full id is 'a7S7j00000011qaEAA' format (a7S+anycharacterornumber). The char of Id is 15 or 18 long

So for I tried this

string str =  'This is a design number D-123989 and Id is a7S7j00000011qaEAA and 90090';
Pattern p1 = Pattern.compile('a7S([a-zA-Z0-9]{12,15})');
Pattern p = Pattern.compile('D-([0-9]+)');
Matcher pm = p.matcher(str);    
Matcher pm1 = p1.matcher(str);    
string res;
if (pm.find()) {
    res = 'match = ' + pm.group(1);
    System.debug(res); //It should give D-123989 but giving 123989
} else {
    System.debug('No match');
}
if (pm1.find()) {
    res = 'match = ' + pm1.group(1);  //It should give a7S7j00000011qaEAA  but not matching
    System.debug(res);
} else {
    System.debug('No match');
}

The first one is a success and it's returning 123989 instead of D-123989 and the second one Id matching is not working. Need help here!

2
  • It is likely because of the round braces, you have created capturing group so the pattern D-[0-9]+ should work.
    – manjit5190
    Mar 21, 2021 at 7:00
  • Any suggestion on Id one
    – bharath
    Mar 21, 2021 at 7:45

2 Answers 2

2

When you use parentheses in a regex, you create a "capture group". These groups are numbered from 1 to x, where x is the number of capture groups you create this way. Group "0" is a special group that represents the entire match. As such, you normally want to use group "0".

string str =  'This is a design number D-123989 and Id is a7S7j00000011qaEAA and 90090';
Pattern p1 = Pattern.compile('a7S([a-zA-Z0-9]{12,15})');
Pattern p = Pattern.compile('D-([0-9]+)');
Matcher pm = p.matcher(str);    
Matcher pm1 = p1.matcher(str);    
string res;
if (pm.find()) {
    res = 'match = ' + pm.group(0);
    System.debug(res); // 07:11:06:026 USER_DEBUG [9]|DEBUG|match = D-123989
} else {
    System.debug('No match');
}
if (pm1.find()) {
    res = 'match = ' + pm1.group(0); 
    System.debug(res); // 07:11:06:027 USER_DEBUG [15]|DEBUG|match = a7S7j00000011qaEAA
} else {
    System.debug('No match');
}
0

It is likely because of the round braces, you have created capturing group.

So if you remove round braces from both patterns it should work i.e. D-[0-9]+ and a7S[a-zA-Z0-9]{12,15} should be the correct patterns.

2
  • string str = 'This is a design number D-123989 and Id is a7S7j00000011qaEAA and 90090'; Pattern p1 = Pattern.compile('a7S[a-zA-Z0-9]{12,15}'); Matcher pm1 = p1.matcher(str); Boolean found; System.debug('Find? ' + (found = pm1.find())); // (DOES NOT) FAIL HERE if(found) { string res1; if (pm1.find()) { res1 = 'match = ' + pm1.group(1); //It should give a7S7j00000011qaEAA but not matching System.debug(res1); } else { System.debug('No match'); } }
    – bharath
    Mar 21, 2021 at 8:39
  • Its not matching
    – bharath
    Mar 21, 2021 at 8:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .