Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.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 如何找到;B"+;最多6个标志_Java_Regex - Fatal编程技术网

Java 如何找到;B"+;最多6个标志

Java 如何找到;B"+;最多6个标志,java,regex,Java,Regex,我需要验证,如果模式有字母“B”和后面最多六个符号(字母和数字)。例如:我们有abcdB1234B123456。找到的答案应该是:B1234和B123456 我做了这个图案: [^B]{1,6} public static void main(String[] args) { final Pattern pattern = Pattern.compile("B[aAc-zC-Z0-9]{0,6}"); final String string = " abcdB1234B1234

我需要验证,如果模式有字母“B”和后面最多六个符号(字母和数字)。例如:我们有
abcdB1234B123456
。找到的答案应该是:
B1234
B123456

我做了这个图案:

[^B]{1,6}
public static void main(String[] args) {
    final Pattern pattern = Pattern.compile("B[aAc-zC-Z0-9]{0,6}");
    final String string = " abcdB1234B123456";
    final Matcher matcher = pattern.matcher(string);
    while (matcher.find()) {
        System.out.println(matcher.group());
    }
}

但是它并不精确。

这个模式呢:

[^B]{1,6}
public static void main(String[] args) {
    final Pattern pattern = Pattern.compile("B[aAc-zC-Z0-9]{0,6}");
    final String string = " abcdB1234B123456";
    final Matcher matcher = pattern.matcher(string);
    while (matcher.find()) {
        System.out.println(matcher.group());
    }
}
输出:

B1234
B123456
请尝试以下代码:

String data = "abcdB1234B123456";
Pattern pattern = Pattern.compile("B[aAc-zC-Z\\d]{0,6}");

Matcher matcher = pattern.matcher(data);
while (matcher.find()) {
    // Indicates match is found. Do further processing
    System.out.println(matcher.group());
}

那么
B1234B1
呢?这与“B”匹配,后面最多有六个符号(字母和数字),不是吗?你是对的,但是(据我所知,在正则表达式中,如果某个东西被“使用”了,那么它不会再次用于搜索),现在我需要我所写的结果。但你的问题是好的,我希望,在这个项目中,这不会在将来发生;)不。什么都不匹配。它应该是@Makoto。在粘贴到这里之前,我已经测试了代码。请检查您是否遗漏了一些内容。当我将它复制到IntelliJ时,我有一些多余的反斜杠。不过,在这篇文章被编辑之前,我不能取消我的投票。他说最多六个字符。您的字符串需要一个字符atm-这不是“如果模式有字母“B”且后面最多有六个符号(字母和数字),我需要验证”的要求-我认为该单词并指示至少一个字符。如果您希望允许小写B,请将模式更改为
B[a-zAC-Z0-9]{0,6}