Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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_Expression - Fatal编程技术网

Java正则表达式模式没有';不要按照在线测试工具所说的去做

Java正则表达式模式没有';不要按照在线测试工具所说的去做,java,regex,expression,Java,Regex,Expression,我对Java中的正则表达式有一个问题。应匹配以下各项: 2x 1 piece 63x 9 pieces 4x 1 piece 1 piece 23 pieces 使用此正则表达式: ((\w+)x\s)*(\w+)\s*(\w*) 众所周知,我们必须在Java中转义字符串。我逃离了正则表达式,我试着使用这个: String regex = "((\\w+)x\\s)*(\\w+)\\s*(\\w*)"; 现在我的问题来了:所有正则表达式的在线服务都将我的模式标记为有效,java除外。他们没

我对Java中的正则表达式有一个问题。应匹配以下各项:

2x 1 piece
63x 9 pieces
4x 1 piece
1 piece
23 pieces
使用此正则表达式:

((\w+)x\s)*(\w+)\s*(\w*)
众所周知,我们必须在Java中转义字符串。我逃离了正则表达式,我试着使用这个:

String regex = "((\\w+)x\\s)*(\\w+)\\s*(\\w*)";
现在我的问题来了:所有正则表达式的在线服务都将我的模式标记为有效,java除外。他们没有标记什么可能是错误的,所以我不能真正看到我的问题。这是我试图在Java中使用的代码:

String regex = "((\\w+)x\\s)*(\\w+)\\s*(\\w*)";
Pattern r = Pattern.compile(regex);
Matcher m = r.matcher(someClassWithMethods.text());
int multiplier=0;
int value= 0;
String supplement = "";
if (m.find( )) {
    multiplier= Integer.parseInt(m.group(2));
    value= Integer.parseInt(m.group(3));    
    supplement = m.group(4);
}
我调试了整个过程,以查看发生了什么,所有变量都如预期的那样,但我仍然得到一个空组。这个正则表达式怎么了

编辑

由于这些评论,我已经改变了一些事情,并且用一个附加的if子句捕获了我的NumberException。现在我仍然没有得到匹配的结果。那会是什么? 这是我的新代码:

String regex = "(?:(\\w+)x\\s)?(\\d+\\s+)(pieces?)";
Pattern r = Pattern.compile(regex);
Matcher m = r.matcher(quantityCell.text());
int quantityMultiplier = 0;
int quantity = 0;
String supplement = "";
if (m.find( )) {
    if(m.group(1) != null){ 
            quantityMultiplier = Integer.parseInt(m.group(1));
    }
    quantity = Integer.parseInt(m.group(2));    
    supplement = m.group(3);
}

你的正则表达式很奇怪:

  • \w+
    当您只对前两个实例中的数字感兴趣时,为什么要匹配“单词字符”
  • ((\w+)x\s)
    为什么这是一个捕获组?你不想要结果
  • ((\w+x\s)*
    为什么会重复?你期望多重乘数吗?若存在多个乘法器,则正则表达式仅捕获最后一个乘法器
让我们试试这个:

(?:(\d+)x\s)?(\d+)\s(\w*)
由于第一次捕获是可选的,因此如果不存在,它将是
null
,因此您需要对此进行检查

public static void main(String[] args) {
    test("2x 1 piece");
    test("63x 9 pieces");
    test("4x 1 piece");
    test("1 piece");
    test("23 pieces");
}
private static void test(String input) {
    String regex = "(?:(\\d+)x\\s)?(\\d+)\\s(\\w*)";
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(input);
    if (m.find()) {
        int multiplier = (m.group(1) != null ? Integer.parseInt(m.group(1)) : -1);
        int value = Integer.parseInt(m.group(2));
        String supplement = m.group(3);
        System.out.printf("%d, %d, '%s'%n", multiplier, value, supplement);
    }
}
输出

2,1,“件”
63,9,“碎片”
4,1,“一块”
-1,1,“件”
-1,23,“碎片”

您所说的“空组”是什么意思?您得到的确切输出是什么?我试过了,对我来说效果很好…你的正则表达式真的不好。为什么不使用
(?:\\w+x\\s)?\\d+\\s+parties?
(未测试的代码)我猜是NumberFormatException?但是对于您的代码(如果不是while),我认为不会出现这种异常。我的意思是matcher组的值不是我所期望的。如果我调试我的程序,我将收到一个具有以下值的组:[0,1,-1,-1,-1,0,1,1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1]。组数组中的位置1很好,因为它是“1件”中匹配的1。但是“碎片”这个词存储在哪里呢?看起来好像不匹配。@Therealnox:我不知道你想在这里做什么。如果您指出方法名,我可以解释模式类中的代码,但您没有提到方法名。如果要使代码正常工作,不需要将其调试到模式类中。向我们展示一个测试用例,其中您的实际值不是您的期望值。这就是我等待的真正答案。多谢各位。我有一段时间没有使用正则表达式了。这就是为什么我没有想到非捕获组。我使用了\w+,因为我第一次检查输出时没有单独处理“x”字符,后来忘记了更改它。祝你过得愉快!:)