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

Java正则表达式组

Java正则表达式组,java,regex,find,matcher,regex-greedy,Java,Regex,Find,Matcher,Regex Greedy,我需要一个表达式来提取一些备选方案。输入为: asd11sdf33sdf55sdfg77sdf 我需要11 33和55,但不是77 我首先尝试: .*(((11)|(33)|(55)).*)+.* 所以我只有55分。但是懒惰(不贪婪) 我只有11个。如何获得全部 将托马斯使用(?!77)(\d\d)作为模式和而(m.find()){m.group(1)}其中m是匹配器组是固定的,您不能在组上使用“+”来获得匹配列表。您必须使用循环来执行此操作: Pattern p = Pattern

我需要一个表达式来提取一些备选方案。输入为:

asd11sdf33sdf55sdfg77sdf
我需要11 33和55,但不是77

我首先尝试:

.*(((11)|(33)|(55)).*)+.*
所以我只有55分。但是懒惰(不贪婪)

我只有11个。如何获得全部


将托马斯使用
(?!77)(\d\d)
作为
模式
而(m.find()){m.group(1)}
其中
m
匹配器
组是固定的,您不能在组上使用“+”来获得匹配列表。您必须使用循环来执行此操作:

    Pattern p = Pattern.compile("((11)|(33)|(55))");
    Matcher m = p.matcher("asd11sdf33sdf55sdfg77sdf");
    int start = 0;
    List<String> matches = new ArrayList<String>();
    while (m.find()) {
        matches.add(m.group());
    }
    System.err.println("matches = " + matches);
Pattern p=Pattern.compile(((11)|(33)|(55));
匹配器m=p.Matcher(“asd11sdf33sdf55sdfg77sdf”);
int start=0;
列表匹配项=新的ArrayList();
while(m.find()){
匹配。添加(m.group());
}
System.err.println(“matches=“+matches”);
尝试使用

.*?(11|33|55)
作为regexp编译模式,并在fge的答案中使用循环。(我认为他的答案更具普遍性和意义

这是因为regexp中的.*或(11 | 33 | 55)之后的内容与11之后的整个字符串匹配。(如果使用贪婪匹配,则.*before(11 | 33 | 55)将与55之前的整个字符串匹配…仅仅因为它是贪婪的)

这样,您将得到一个匹配项,其匹配项(1)是11,find()是11之后字符串的匹配项


测试您的要求不清楚。您想断言字符串中存在11、33和55,但77不存在吗?数字的顺序是否相关?您实际想要实现什么?我想要所有11、33或55。在aa33ss33dd33ee中,我需要使用3次33I:Pattern p=Pattern.compile((11 | 33 | 55));Matcher m=p.Matcher(“asd11sdf33sdf55sdfg77sdf”);int start=0;List matches=new ArrayList();while(m.find()){matches.add(m.group());}System.err.println(“matches=“+matches”);它可以查找任何两位数,而不仅仅是11、33或55(尽管我不清楚@tricpod需要什么正则表达式来完成这个任务)。哦,我的问题不够精确。11 33 55只是一个例子。那么这就是正确的解决方案。+1是今天水晶球的最佳使用。
.*?(11|33|55)