-1

As an example let's imagine I'm searching for a specific group of number inside a name

[3363]Burn the Witch Year 1958

In this case there are 2 groups of number of the same length, and I need to get the one inside the brackets. How can search for a group of number of a specific length, that are inside a brackets without getting the brackets in the answer?

regexChecker("\\[\\d{3,4}\\]",longString);

The output of the example code is [3347].

The code im using as practice is:

public class PracticaPatrones {
    public static void main(String[] args) {
        String longString = "";

        String[] Names = {"[3358]Maoujou de Oyasumi 1", "[3347]King's Raid; Ishi wo Tsugumono-tachi 2", "[3363]Burn the Witch 3", "[3283]Hachi-nan tte, Sore wa Nai deshou! 1", "[391]Denpa Onna to Seishun Otoko 1", "[0587] Onii-chan Dakedo Ai Sae Areba Kankeinai yo ne 11", "[870] Onii-chan Dakedo Ai Sae Areba Kankeinai yo ne 11"};

        System.out.println(Names.length);
        System.out.println("Bandera");

        for (int i = 0; i < Names.length; i++) {
            longString = Names[i];

            regexChecker("\\[\\d{3,4}\\]", longString);
        }
    }

    public static void regexChecker(String theRegex, String str2Check) {
        Pattern checkRegex = Pattern.compile(theRegex);
        Matcher regexMatcher = checkRegex.matcher(str2Check);

        while (regexMatcher.find()) {
            if (regexMatcher.group().length() != 0) {
                System.out.println(regexMatcher.group().trim());
            }

            System.out.println("start index : " + regexMatcher.start());
            System.out.println("end index : " + regexMatcher.end());
        }
    }
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578

2 Answers2

2

You can use the regex, (?<=\[)\d{3,4}(?=\]) which can be explained as follows:

  1. (?<=\[) looks behind for [
  2. \d{3,4} looks for 3 to 4 digits (it's already there in your question)
  3. (?=\]) looks ahead for ]

Demo:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        Pattern pattern = Pattern.compile("(?<=\\[)\\d{3,4}(?=\\])");
        Matcher matcher = pattern.matcher("[3358]Maoujou de Oyasumi 1");
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }
}

Output:

3358
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
1

You could also put the brackets in non-capturing groups: (?:) like this: (?:\[)(\d{3,4})(?:\]) and check group #1.

Online demo

Java code needs minor modifications:

public static void regexChecker(String theRegex, int groupId, String str2Check) {

    Pattern checkRegex = Pattern.compile(theRegex);
    
    Matcher regexMatcher = checkRegex.matcher(str2Check);
    
    System.out.printf("Input: '%s' - ", str2Check);
    while(regexMatcher.find()) {
        if (regexMatcher.group().length() > groupId) {
            System.out.printf("Found groupId %d: %s ", groupId, regexMatcher.group(groupId));
        }
        System.out.printf("between %d, %d%n", regexMatcher.start(), regexMatcher.end());
    }
}

Tests:

String[] names = {
    "[3358]Maoujou de Oyasumi 1",
    "[3347]King's Raid; Ishi wo Tsugumono-tachi [232]",
    "[3363]Burn the Witch 3",
    "[3283]Hachi-nan tte, Sore wa Nai deshou! 1445",
    "[391]Denpa Onna to Seishun Otoko 1",
    "[0587] Onii-chan Dakedo Ai Sae Areba Kankeinai yo ne 11",
    "[870] Onii-chan Dakedo Ai Sae Areba Kankeinai yo ne 11"     
};

for (String name : names) {
    regexChecker("(?:\\[)(?<id>\\d{3,4})(?:\\])", 1, name);
}

Output

Input: '[3358]Maoujou de Oyasumi 1' - Found groupId 1: 3358 between 0, 6
Input: '[3347]King's Raid; Ishi wo Tsugumono-tachi [232]' - Found groupId 1: 3347 between 0, 6
Found groupId 1: 232 between 43, 48
Input: '[3363]Burn the Witch 3' - Found groupId 1: 3363 between 0, 6
Input: '[3283]Hachi-nan tte, Sore wa Nai deshou! 1445' - Found groupId 1: 3283 between 0, 6
Input: '[391]Denpa Onna to Seishun Otoko 1' - Found groupId 1: 391 between 0, 5
Input: '[0587] Onii-chan Dakedo Ai Sae Areba Kankeinai yo ne 11' - Found groupId 1: 0587 between 0, 6
Input: '[870] Onii-chan Dakedo Ai Sae Areba Kankeinai yo ne 11' - Found groupId 1: 870 between 0, 5

In Java 9 it is possible to use stream of MatchResult to retrieve the list of all matches:

public static List<String> getMatches(String theRegex, int groupId, String str2Check) {
    Pattern checkRegex = Pattern.compile(theRegex);

    return checkRegex.matcher(str2Check)
            .results()
            .filter(r -> r.groupCount() >= groupId)
            .map(r -> r.group(groupId))
            .collect(Collectors.toList());
}
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42