Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 对于正则表达式,我怎样才能得到括号内的数字?_Java_Regex - Fatal编程技术网

Java 对于正则表达式,我怎样才能得到括号内的数字?

Java 对于正则表达式,我怎样才能得到括号内的数字?,java,regex,Java,Regex,作为一个例子,让我们假设我正在搜索一个名称中的一组特定数字 [3363]1958年焚烧女巫 在本例中,有两组相同长度的数字,我需要得到括号内的数字。如何搜索括号内的一组特定长度的数字而不获取答案中的括号 regexChecker("\\[\\d{3,4}\\]",longString); 示例代码的输出为[3347] 我实际使用的代码是: public class PracticaPatrones { public static void main(String[]

作为一个例子,让我们假设我正在搜索一个名称中的一组特定数字

[3363]1958年焚烧女巫 在本例中,有两组相同长度的数字,我需要得到括号内的数字。如何搜索括号内的一组特定长度的数字而不获取答案中的括号

regexChecker("\\[\\d{3,4}\\]",longString);
示例代码的输出为[3347]

我实际使用的代码是:

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

也可以将括号放在非捕获组中:?:如下:?:\[\d{3,4}?:\]并检查组1

Java代码需要稍作修改:

public static void regexCheckerString theRegex,int groupId,String str2 check{ Pattern checkRegex=Pattern.compileteregex; Matcher regexMatcher=checkRegex.matcherstr2Check; System.out.printfInput:“%s”-,str2Check; whileregexMatcher.find{ 如果regexMatcher.group.length>groupId{ System.out.printfound groupId%d:%s,groupId,regexMatcher.groupId; } %d,%d%n,regexMatcher.start,regexMatcher.end之间的System.out.printfb; } } 测试:

字符串[]名称={ [3358]Maoujou de Oyasumi 1, [3347]国王的突袭;石和村野大枝[232], [3363]焚烧女巫3, [3283]Hachi nan tte,Sore wa Nai deshou!1445, [391]Denpa Onna至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 }; 对于字符串名称:名称{ regexChecker?:\\[?\\d{3,4}?:\\],1,名称; } 输出

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
在Java 9中,可以使用MatchResult流检索所有匹配项的列表:

公共静态列表getMatchesString theRegex、int-groupId、字符串str2检查{ Pattern checkRegex=Pattern.compileteregex; 返回checkRegex.matcherstr2Check 后果 .filterr->r.groupCount>=groupId .mapr->r.groupid .toList; }
你可以使用正则表达式,或者干脆不把方括号放在括号里。\[\d{3,4}\]就是这样做的。@JohnKugelman,你说得对,这也行:
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());
        }
    }
}
3358