TheSloth
The Slowest One
Hey man! Do you still participate in these?There is also project Euler. Also look forward to Advent of Code every year.
Hey man! Do you still participate in these?There is also project Euler. Also look forward to Advent of Code every year.
((/?)([A-Z]{3})([0-9]{1,15}))
ABC123/DEF1223456/GHI12345678980834
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExCHGSTagPattern {
public static void main(String[] args) {
String input1 = "ABC123";
String input2 = "ABC123/DEF1223456/GHI12345678980834";
Pattern p = Pattern.compile("((/?)([A-Z]{3})([0-9]{1,15}))");
Matcher m1 = p.matcher(input1);
Matcher m2 = p.matcher(input2);
System.out.println("output1-"+m1.matches());
while(m2.find()) {
System.out.println(m2.group()+"-"+p.matcher(m2.group()).matches());
}
System.out.println("output2-"+m2.matches());
}
}
I am trying but its not working in IDE. i did match the strings on *regex101.com/I think you haven't escaped the first forward slash.
I matched all the groups in your string using this regex:
([A-Z]{3}\d{1,15})\/?
Edit: Replaced\d+
with\d{1,15}
since you have a constraint on the number of digits.
Pattern p = Pattern.compile("([A-Z]{1,3}[0-9]{1,15}/?)+");
[A-Z]{1,3}
--> any character from A to Z which will repeat for minimum 1 to maximum 3 times [0-9]{1,15}
--> any number from 0 to 9 which will repeat for minimum 1 to maximum 15 times/?
--> / can come for zero or more times [A-Z]{1,3}[0-9]{1,15}/?
--> ([A-Z]{1,3}[0-9]{1,15}/?)+
In Java you have to escape the backslashes for regex Strings.I am trying but its not working in IDE. i did match the strings on *regex101.com/
View attachment 22926
private static final String pattern = "([A-Z]{3}\\d{1,15})\\/?";
class Main {
private static final String pattern = "([A-Z]{3}\\d{1,15})\\/?";
private static final String input = "ABC123/DEF1223456/GHI12345678980834";
public static void main(String[] args) {
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(input);
while(m.find()) {
System.out.println(m.group(1));
}
}
}
ABC123
DEF1223456
GHI12345678980834
group(1)
will match the bracketed group in the regex. Updated the answer to reflect that.if you add m.matches() line at the end, it returns false. This is exactly my question, we are able to see the group from the entire input which matches, I am wondering why m.matches() return false at the end.In Java you have to escape the backslashes for regex Strings.
private static final String pattern = "([A-Z]{3}\\d{1,15})\\/?";
Java:class Main { private static final String pattern = "([A-Z]{3}\\d{1,15})\\/?"; private static final String input = "ABC123/DEF1223456/GHI12345678980834"; public static void main(String[] args) { Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(input); while(m.find()) { System.out.println(m.group(1)); } } }
Yields:
Code:ABC123 DEF1223456 GHI12345678980834
With some group adjustment, the ending slashes can be excluded.
Edit: Apparently, simply usinggroup(1)
will match the bracketed group in the regex. Updated the answer to reflect that.
It worked! Adding the + symbol at the end of the entire group returns true. You are a saviour <3Try with this website to test with different inputs which can come up - *regex101.com/r/4dq5xR/1
Java:Pattern p = Pattern.compile("([A-Z]{1,3}[0-9]{1,15}/?)+");
I think this code should work
Explanation for my code --
[A-Z]{1,3}
--> any character from A to Z which will repeat for minimum 1 to maximum 3 times
[0-9]{1,15}
--> any number from 0 to 9 which will repeat for minimum 1 to maximum 15 times
/?
--> / can come for zero or more times
this regex can possibly match with strings like[A-Z]{1,3}[0-9]{1,15}/?
-->
ABC123
DEF123/
X1
YZ23/
XYZ123456789012345/
But we want the above group to come one or more times, so we can mention it as group and add + for one or more times
final regex is -([A-Z]{1,3}[0-9]{1,15}/?)+
View attachment 22927
\d
instead of [0-9]
since it matches single digits, while the [] notation works if you are creating an alphanumeric pattern.