0
[0-6, 1-3][01-20, 22-23]22/123

From the above input, I would like to extract the following the following texts.

0-6, 1-3
01-20, 22-23
22
123

The following code snippets extracts the required texts excepts the first one.

    Pattern depArrHours = Pattern.compile("^(\\[(.+)\\]){2}(.+)\\/(.+)$");
    Matcher matcher = depArrHours.matcher("[0-6, 1-3][01-20, 22-23]22/123");
    if (matcher.matches()) {
        System.out.println(matcher.group(0));
        System.out.println(matcher.group(1));
        System.out.println(matcher.group(2));
        System.out.println(matcher.group(3));
        System.out.println(matcher.group(4));
    }

Output:

[0-6, 1-3][01-20, 22-23]22/123
[01-20, 22-23]
01-20, 22-23
22
123

Can you please help me to fix my regex pattern to extract the first part also(0-6, 1-3)?

5 Answers 5

1

You could try specifying each (\\[(.+)\\]) separate instead of {2}:

Pattern depArrHours = Pattern.compile("^(\\[(.+)\\])(\\[(.+)\\])(.+)\\/(.+)$");
Matcher matcher = depArrHours.matcher("[0-6, 1-3][01-20, 22-23]22/123");
if (matcher.matches()) {
    System.out.println(matcher.group(0));
    System.out.println(matcher.group(1));
    System.out.println(matcher.group(2));
    System.out.println(matcher.group(3));
    System.out.println(matcher.group(4));
    System.out.println(matcher.group(5));
    System.out.println(matcher.group(6));
}

Output :

[0-6, 1-3][01-20, 22-23]22/123
[0-6, 1-3]
0-6, 1-3
[01-20, 22-23]
01-20, 22-23
22
123
Sign up to request clarification or add additional context in comments.

Comments

1

You should specify each of the first 2 groups separately:

Pattern depArrHours = Pattern.compile("^(\\[(.+)\\])(\\[(.+)\\])(.+)\\/(.+)$");

Comments

1

Try,

    String input="[0-6, 1-3][01-20, 22-23]22/123";
    String[] arr=input.replace('[', ' ').split("[\\]/]");
    for (String string : arr) {
        System.out.println(string.trim());
    }

Comments

1

Try the pattern

 \\[([^\\]]*)\\]|(([0-9]*)/([0-9]*))

and use it with matcher.find()

Pattern depArrHours = Pattern.compile("\\[([^\\]]*)\\]|(([0-9]*)/([0-9]*))");
Matcher matcher = depArrHours.matcher("[0-6, 1-3][01-20, 22-23]22/123");
while (matcher.find()) {
    String group = matcher.group(1);
        if (group == null) {
            // matched 22/123
            System.out.println(matcher.group(3));
            System.out.println(matcher.group(4));
        } else {
            // matched [0-6, 1-3] or [01-20, 22-23]
            System.out.println(group);
        }
}

Output

0-6, 1-3
01-20, 22-23
22
123

Comments

1

Try

    String  k ="[0-6, 1-3][01-20, 22-23]22/123";
    Pattern p = Pattern.compile("\\[([^\\]]*)\\]|([0-9]*)/([0-9]*)");
    Matcher m = p.matcher(k);
    while(m.find()){
        System.out.println((m.group(1)!=null)?m.group(1):m.group(2)+"\n"+m.group(3));   
    }

The Regex \\[([^\\]]*)\\]|([0-9]*)/([0-9]*) can be represented as

enter image description here

Output:

0-6, 1-3
01-20, 22-23
22
123

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.