也许有人可以帮助我。我试图在Java代码中包含一个正则表达式以匹配除ZZ78之外的所有字符串。我想知道我的正则表达式中缺少什么。

输入字符串为str =“ ab57cdZZ78efghZZ7ij @ klmZZ78noCODpqrZZ78stuvw27z @ xyzZZ78”

而我正在尝试使用此正则表达式(?:(?![ZZF8])。)*,但是如果您在http://regexpal.com/中进行测试
这个针对字符串的正则表达式,您会发现它不能完全正常工作。

str = new String ("ab57cdZZ78efghZZ7ij@klmZZ78noCODpqrZZ78stuvw27z@xyzZZ78");
Pattern pattern = Pattern.compile("(?:(?![ZZ78]).)*");


匹配的字符串应该是

ab57cd
efghZZ7ij@klm
noCODpqr
stuvw27z@xyz


更新:

您好Avinash Raj和Chthonic Project。非常感谢您的帮助和提供的解决方案。

我本来是使用split方法的,但是我试图避免得到空字符串
例如,当分隔符字符串位于主字符串的开头或结尾时。

然后,我以为正则表达式可以帮助我提取除“ ZZ78”以外的所有内容,从而避免这种情况
输出结果为空。

下面我展示了使用拆分方法(Chthonic的)和正则表达式(Avinash的)的代码都产生空
如果未使用注释的“ if()”条件,则为字符串。

使用那些“ if()”是不打印空字符串的唯一方法吗?或者可能是正则表达式
调整了一点以匹配非空字符串?

到目前为止,这是我测试过的代码:

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

    public class RegexTest {
        public static void main(String[] args) {
            System.out.println("########### Matches with Split ###########");
            String str = "ZZ78ab57cdZZ78efghZZ7ij@klmZZ78noCODpqrZZ78stuvw27z@xyzZZ78";
            for (String s : str.split("ZZ78")) {
                //if ( !s.isEmpty() ) {
                    System.out.println("This is a match <<" + s  + ">>");
                //}
            }
            System.out.println("##########################################");

            System.out.println("########### Matches with Regex ###########");
            String s = "ZZ78ab57cdZZ78efghZZ7ij@klmZZ78noCODpqrZZ78stuvw27z@xyzZZ78";
            Pattern regex = Pattern.compile("((?:(?!ZZ78).)*)(ZZ78|$)");
            Matcher matcher = regex.matcher(s);
            while(matcher.find()){
                //if ( !matcher.group(1).isEmpty() ) {
                    System.out.println("This is a match <<" + matcher.group(1) + ">>");
                //}
            }
        }
    }

**and the output (without use the "if()´s"):**
########### Matches with Split ###########
This is a match <<>>
This is a match <<ab57cd>>
This is a match <<efghZZ7ij@klm>>
This is a match <<noCODpqr>>
This is a match <<stuvw27z@xyz>>
##########################################
########### Matches with Regex ###########
This is a match <<>>
This is a match <<ab57cd>>
This is a match <<efghZZ7ij@klm>>
This is a match <<noCODpqr>>
This is a match <<stuvw27z@xyz>>
This is a match <<>>


到目前为止,感谢您的帮助。

提前致谢

更新#2:

出色的答案和解决方案。现在,它的工作非常好。这是我用这两种解决方案测试过的最终代码。

再次非常感谢。

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

public class RegexTest {
    public static void main(String[] args) {
        System.out.println("########### Matches with Split ###########");
        String str = "ZZ78ab57cdZZ78efghZZ7ij@klmZZ78noCODpqrZZ78stuvw27z@xyzZZ78";
        Arrays.stream(str.split("ZZ78")).filter(s -> !s.isEmpty()).forEach(System.out::println);

        System.out.println("##########################################");

        System.out.println("########### Matches with Regex ###########");
        String s = "ZZ78ab57cdZZ78efghZZ7ij@klmZZ78noCODpqrZZ78stuvw27z@xyzZZ78";
        Pattern regex = Pattern.compile("((?:(?!ZZ78).)*)(ZZ78|$)");
        Matcher matcher = regex.matcher(s);
        ArrayList<String> allMatches = new ArrayList<String>();
        ArrayList<String> list = new ArrayList<String>();
        while(matcher.find()){
            allMatches.add(matcher.group(1));
        }
        for (String s1 : allMatches)
            if (!s1.equals(""))
                list.add(s1);

        System.out.println(list);
    }
}

And output:
########### Matches with Split ###########
ab57cd
efghZZ7ij@klm
noCODpqr
stuvw27z@xyz
##########################################
########### Matches with Regex ###########
[ab57cd, efghZZ7ij@klm, noCODpqr, stuvw27z@xyz]

最佳答案

您必须删除字符类,因为[ZZ78]匹配给定列表中的单个字符。 (?:(?!ZZ78).)*不能满足您的要求。将此ab57cdZZ78视为输入字符串。首先,此(?:(?!ZZ78).)*匹配字符串ab57cd,然后尝试匹配以下Z并检查条件(?!ZZ78),这意味着匹配任何字符,但不匹配ZZ78。因此它无法匹配以下Z,接下来正则表达式引擎移至下一个字符Z并检查此(?!ZZ78)条件。由于第二个Z后面没有Z78,因此此Z被正则表达式引擎匹配。

String s = "ab57cdZZ78efghZZ7ij@klmZZ78noCODpqrZZ78stuvw27z@xyzZZ78";
Pattern regex = Pattern.compile("((?:(?!ZZ78).)*)(ZZ78|$)");
Matcher matcher = regex.matcher(s);
while(matcher.find()){
        System.out.println(matcher.group(1));
}


输出:

ab57cd
efghZZ7ij@klm
noCODpqr
stuvw27z@xyz


说明:


((?:(?!ZZ78).)*)捕获任何字符,但不捕获ZZ78零次或多次。
(ZZ78|$)还将以下ZZ78或行锚的末尾捕获到组2中。
组索引1包含除ZZ78以外的单个或一组字符


更新:

String s = "ZZ78ab57cdZZ78efghZZ7ij@klmZZ78noCODpqrZZ78stuvw27z@xyzZZ78";
Pattern regex = Pattern.compile("((?:(?!ZZ78).)*)(ZZ78|$)");
Matcher matcher = regex.matcher(s);
ArrayList<String> allMatches = new ArrayList<String>();
ArrayList<String> list = new ArrayList<String>();
while(matcher.find()){
    allMatches.add(matcher.group(1));
}
for (String s1 : allMatches)
    if (!s1.equals(""))
        list.add(s1);

System.out.println(list);


输出:

[ab57cd, efghZZ7ij@klm, noCODpqr, stuvw27z@xyz]

10-08 14:02