You need to sign in to do that
Don't have an account?
brijender singh rathore 16
How to extract a substring from string using regex
I want regex for this format and filter out 123456 if match the current format
PO # 123456
PO# 123456
PO #123456
P.O. # 123456
P.O.# 123456
P.O. #123456
We have to filter substring from string notes__c field. my regex is only working for PO #123456 and P.O. #123456
tried PO# (\\S+)\\s not working
PO # 123456
PO# 123456
PO #123456
P.O. # 123456
P.O.# 123456
P.O. #123456
We have to filter substring from string notes__c field. my regex is only working for PO #123456 and P.O. #123456
tried PO# (\\S+)\\s not working
Matcher rm = r.matcher(oOrder.Notes__c); Pattern r = Pattern.compile('PO #(\\S+)\\s'); if(rm.find()) { string res2 = rm.group(1); oOrder.test__c = res2; }
The key thing to note here is the optional whitespace. Whitespace can appear before and/or after the #. Whenever you see a pattern like this, you usually need \s* (if you allow multiple whitespaces) or \s? (if you only allow one or more whitespace).
The first part can either be PO or P.O.. We can write these two alternatives with the | token. Remember to escape the .s.
The whole regex looks like this: Here, I assumed the thing you want to capture matches \d+. If that part can contain letters, change the capture group accordingly.
Demo
To write the regex as a string literal, you need to escape the backslashes: Hope this helps.
Kindly mark this as solved if the reply was helpful.
Thanks,
Nagendra
I have Modified the my code
it is covering everything except
PO # 123456