Siva Arunachalam
Siva Arunachalam

Reputation: 7740

Extract a String using RegEx

[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)?

Upvotes: 0

Views: 74

Answers (5)

Rakesh KR
Rakesh KR

Reputation: 6527

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

Upvotes: 1

René Link
René Link

Reputation: 51363

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

Upvotes: 1

Nishan
Nishan

Reputation: 2871

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

Upvotes: 1

Masudul
Masudul

Reputation: 21961

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

Upvotes: 1

Szymon
Szymon

Reputation: 43023

You should specify each of the first 2 groups separately:

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

Upvotes: 1

Related Questions