Regular expression to limit number of digits - regex

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";

Related

" java regex" match words with numbers and specials caracters

I have this regex :
"([ ]?[a-zA-Z]{3,})"
but when I try match these words :
"sol perro idea \ncaballo\ndo7\ntres\n tr_es\n8cuatro\ncinco.\n3pesos\n$dollar$\nccc\ncoton\nH7T\n chien#\na-z\n"
i get these matchs:
sol
perro
idea
caballo
tres
cuatro
cinco
pesos
dollar
ccc
coton
chien
please how i change my regex ???? if i want 8cuatro\n 3pesos\n $dollar$\n and chien#\n not matched....thanks lot of
bye
If you don't want to match the leading space, you can omit that from the pattern, and you can also omit the capture group if you want matches only.
You can assert a whitspace boundary to the left, and at the right side a word boundary followed by asserting not # to the right.
(?<!\S)[a-zA-Z]{3,}\b(?!#)
See a regex demo
In Java:
String regex = "(?<!\\S)[a-zA-Z]{3,}\\b(?!#)";
String string = "sol perro idea \n"
+ "caballo\n"
+ "do7\n"
+ "tres\n"
+ " tr_es\n"
+ "8cuatro\n"
+ "cinco.\n"
+ "3pesos\n"
+ "$dollar$\n"
+ "ccc\n"
+ "coton\n"
+ "H7T\n"
+ " chien#\n"
+ "a-z\n";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println(matcher.group(0));
}
Output
sol
perro
idea
caballo
tres
cinco
ccc
coton

regex for validating input C 200 50

How do i write regex for below?
C 200 50
C/c can be upper case or lower case.
200 - 0 to 200 range
50 - o to 50 range
All three words are separated by space and there can be 1 or more space.
This is what i tried so far.
public static void main(String[] args) {
String input = "C 200 50";
String regex = "C{1} ([01]?[0-9]?[0-9]|2[0-9][0]|20[0]) ([01]?[0-5]|[0-5][0])";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
boolean found = false;
while (matcher.find()) {
System.out.println("I found the text "+matcher.group()+" starting at index "+
matcher.start()+" and ending at index "+matcher.end());
found = true;
}
}
Not sure how to have multiple space, upper or lower first 'C'
If you are validating a string, you must be expecting a whole string match. It means you should use .matches() rather than .find() method as .matches() requires a full string match.
To make c match both c and C you may use Pattern.CASE_INSENSITIVE flag with Pattern.compile, or prepend the pattern with (?i) embedded flag option.
To match one or more spaces, one would use + or \\s+.
To match leading zeros, you may prepend the number matching parts with 0*.
Hence, you may use
String regex = "(?i)C\\s+0*(\\d{1,2}|1\\d{2}|200)\\s+0*([1-4]?\\d|50)";
and then
See the regex demo and a Regulex graph:
See the Java demo:
String input = "C 200 50";
String regex = "(?i)C +0*(\\d{1,2}|1\\d{2}|200) +0*([1-4]?\\d|50)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
boolean found = false;
if (matcher.matches()) {
System.out.println("I found the text "+matcher.group()+" starting at index "+
matcher.start()+" and ending at index "+matcher.end());
found = true;
}
Output:
I found the text C 200 50 starting at index 0 and ending at index 8
If you need a partial match, use the pattern with .find() method in a while block. To match whole words, wrap the pattern with \\b:
String regex = "(?i)\\bC\\s+0*(\\d{1,2}|1\\d{2}|200)\\s+0*([1-4]?\\d|50)\\b";

How to extract a substring from string using regex

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

Salesforce extract substring from string with regex

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');

Java Matcher Error

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);
}