Salesforce extract substring from string with regex -


i developing application in salesforce apex , need extract substring other string. original string:

string str = 'product: multi screen encoder version: 3.51.10 (008) order number: 0030000a9ddy part number: 99-00228-x0-y-ww02-na01 comment: comments'; 

i want extract value of part number using matcher , pattern classes:

pattern p = pattern.compile('part number: (.+)\\s'); matcher pm = p.matcher(str);     if (pm.matches()) {     res = 'match = ' + pm.group(1);     system.debug(res); } else {     system.debug('no match'); } 

but getting no match.

how can fix regex match correctly string

you need use find function instead of matches in if condition.

pattern p = pattern.compile('part number: (\\s+)\\s'); matcher pm = p.matcher(str);     if (pm.find()) {     res = 'match = ' + pm.group(1);     system.debug(res); } else {     system.debug('no match'); } 

\\s+ matches 1 or more non-space characters.

or

pattern p = pattern.compile('part number: (.+?)\\s'); 

Comments

Popular posts from this blog

symfony - InvalidConfigurationException in SecurityExtension.php line 429: No authentication listener registered for firewall "secured_area" -

angular ui router - 10 digest iterations reached in angularjs when using $state.go -

javascript - Handling constructor related functions while testing with mocha and sinon -