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
Post a Comment