Java Queries Here..

TheSloth

The Slowest One
Need help in understanding why reg ex pattern
Code:
((/?)([A-Z]{3})([0-9]{1,15}))
is returning false for input
Code:
ABC123/DEF1223456/GHI12345678980834

Sample code:
Java:
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());
  }
}

@Desmond @khalil1210 @quicky008
 

Desmond

Destroy Erase Improve
Staff member
Admin
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.
 

khalil1210

In the zone
Try 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 1724081351825.png
 
Last edited:

TheSloth

The Slowest One
The problem is I am able to match the regex with online compiler. But for some reason it's not working in IDE. I am using Java 17
 

Desmond

Destroy Erase Improve
Staff member
Admin
I am trying but its not working in IDE. i did match the strings on *regex101.com/

View attachment 22926
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 using group(1) will match the bracketed group in the regex. Updated the answer to reflect that.
 
Last edited:

Desmond

Destroy Erase Improve
Staff member
Admin
BTW, note that regex implementations are not standard and different languages/platforms have slightly different behaviour.
 

TheSloth

The Slowest One
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 using 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.
 

TheSloth

The Slowest One
Try 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
It worked! Adding the + symbol at the end of the entire group returns true. You are a saviour <3
 

Desmond

Destroy Erase Improve
Staff member
Admin
Ok, I misunderstood your question. I thought you wanted to extract the individual components of the string.

Though I would use \d instead of [0-9] since it matches single digits, while the [] notation works if you are creating an alphanumeric pattern.
 
Top Bottom