code
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
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;
}
Try this:
.?(\d+)$
For the example you posted it works.
https://regex101.com/r/eTjbMo/1/
The key thing to note here are 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 whitespace) 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:
(?:PO|P\.O\.)\s*#\s*(\d+)
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:
(?:PO|P\\.O\\.)\\s*#\\s*(\\d+)
Above codes are not working friends
i HAVE MODIFIED MY CODE TO
if(oOrder.Notes__c != null) {
Pattern p = Pattern.compile('PO # (\\S+)\\s');
Pattern q = Pattern.compile('PO# (\\S+)\\s');
Pattern r = Pattern.compile('PO #(\\S+)\\s');
Pattern s = Pattern.compile('P.O. # (\\S+)\\s');
Pattern t = Pattern.compile('P.O.# (\\S+)\\s');
Pattern u = Pattern.compile('P.O. #(\\S+)\\s');
Matcher pm = p.matcher(oOrder.Notes__c);
Matcher qm = q.matcher(oOrder.Notes__c);
Matcher rm = r.matcher(oOrder.Notes__c);
Matcher sm = s.matcher(oOrder.Notes__c);
Matcher tm = t.matcher(oOrder.Notes__c);
Matcher um = u.matcher(oOrder.Notes__c);
system.debug('find = ' +pm.find());
if (pm.find()){
string res = pm.group(1);
oOrder.test__c = res;
}
if(qm.find()){
string res1 = qm.group(1);
oOrder.test__c = res1;
}
if(rm.find()){
string res2 = rm.group(1);
oOrder.test__c = res2;
}
if(sm.find()){
string res3 = sm.group(1);
oOrder.test__c = res3;
}
if(tm.find()){
string res4 = tm.group(1);
oOrder.test__c = res4;
}
if(um.find()){
string res5 = um.group(1);
oOrder.test__c = res5;
}
}
it is covering everything except First one
PO # 123456
Related
Given a string like abab/docId/example-doc1-2019-01-01, I want to use Regex to extract these values:
firstPart = example
fullString = example-doc1-2019-01-01
I have this:
import scala.util.matching.Regex
case class Read(theString: String) {
val stringFormat: Regex = """.*\/docId\/([A-Za-z0-9]+)-([A-Za-z0-9-]+)$""".r
val stringFormat(firstPart, fullString) = theString
}
But this separates it like this:
firstPart = example
fullString = doc1-2019-01-01
Is there a way to retain the fullString and do a regex on that to get the part before the first hyphen? I know I can do this using the String split method but is there a way do it using regex?
You may use
val stringFormat: Regex = ".*/docId/(([A-Za-z0-9])+-[A-Za-z0-9-]+)$".r
||_ Group 2 _| |
| |
|_________________ Group 1 __|
See the regex demo.
Note how capturing parentheses are re-arranged. Also, you need to swap the variables in the regex match call, see demo below (fullString should come before firstPart).
See Scala demo:
val theString = "abab/docId/example-doc1-2019-01-01"
val stringFormat = ".*/docId/(([A-Za-z0-9]+)-[A-Za-z0-9-]+)".r
val stringFormat(fullString, firstPart) = theString
println(s"firstPart: '$firstPart'\nfullString: '$fullString'")
Output:
firstPart: 'example'
fullString: 'example-doc1-2019-01-01'
I am developing an application in Salesforce with Apex and i need to extract a Substring from other string. This is the 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: some comments';
i Want to extract the value of Part Number so i am using the Matcher and 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 i am getting No match.
How can i fix the regex to match correctly my String
You need to use find function instead of matches in the 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 one or more non-space characters.
or
Pattern p = Pattern.compile('Part Number: (.+?)\\s');
Using regex101 I have developed this regex:
^(\S+)\s_(\S)(\S[^;\s]+)?.*
This works great for 99.999% of the time but occasionally it is run against a string containing a 2 char word that should have matched.
For example it would normally capture...
string _asdf = string.empty;
bool _ttfnow;
//$1 = string
//$2 = a
//$3 = sdf
and
//$1 = bool
//$2 = t
//$3 = tfnow
But for some reason this fails to match the third group?
string _qw = string.empty;
//$1 = string
//$2 = q
//$3 =
Again using regex101 if add add a char it suddenly matches so:
string _qwx = string.empty;
//$1 = string
//$2 = q
//$3 = wx
Any ideas? Thank You
^(\S+)\s_(\S)(\S[^;\s]*)?.*
^^
Just change the quantifier.See demo.
https://regex101.com/r/pG1kU1/33
[^;\s]+ change it to [^;\s]*
/^(\S+)\s_(\S)(\S[^;\s]*)?.*/
I am trying to write a regular expression that will only match with qtr1, qtr2, qtr3, qtr4 with help of following regex [q|qtr|qtrs|quarter]+[1-4] but the problem is if i ask something like this "Ficoscore for Q21 2005" a space is added between Q and 21 ie "Ficoscore for Q 21 2005" this not valid.
String regEx = "([q|qtr|qtrs|quarter]+[1-4])";
Pattern pattern = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(userQuerySentence);
System.out.println(matcher.matches());
while (matcher.find()) {
String quarterString = matcher.group();
userQuerySentence = userQuerySentence.replaceAll(quarterString,
(quarterString.substring(0, quarterString.length() - 1) + " " + quarterString.substring(quarterString
.length() - 1)));
}
[q|qtr|qtrs|quarter] is a character class, I guess you want (q|qtr|qtrs|quarter):
String regEx = "(?i)\\b((?:q(?:trs?|uarter)?)[1-4])\\b";
My string:
null[00:14.04]I've /n[00:14.11]got /n[00:14.18]a /n[00:14.25]fee- /n[00:15.02]ling /n
I am trying to obtain every data between [<--->] brackets. Here's my code.
String find = "[(.*?)\\\\]";
Pattern patern = Pattern.compile(find);
Matcher matcher = patern.matcher(intake);
while(matcher.find()){
i++;
matcher.find(i);
int start = matcher.start();
int end = matcher.end();
String group = matcher.group();
}
The first results are:
start = 10
end = 11
group = "."
What I wanted was (Counting on my head)
start = 4
end = 14
group = [00:14.04]
Next is
start = 22
end = 32
group = [00:14.11]
and so on
What is the correct pattern?
You're using wrong escaping. Use this regex:
String find = "\\[(.*?)\\]";
EDIT: Based on your comment:
If you want to capture all items inside square brackets just run your while loop like this:
while(matcher.find()) {
String matched = matcher.group(1);
System.out.printf("Matched Group: [%s]%n", matched);
}