簡體   English   中英

正則表達式從匹配Java代碼中排除單詞

[英]Regex to exclude word from matches java code

也許有人可以幫助我。 我試圖在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”以外的所有內容,從而避免在輸出中出現空結果。

下面,我顯示了如果不使用注釋的“ if()”條件,則使用拆分方法(Chthonic的)和正則表達式(Avinash的)的代碼都將生成空字符串。

使用那些“ 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]

最簡單的方法如下:

public static void main(String[] args) {
    String str = "ab57cdZZ78efghZZ7ij@klmZZ78noCODpqrZZ78stuvw27z@xyzZZ78";
    for (String s : str.split("ZZ78"))
        System.out.println(s);
}

預期的輸出是:

ab57cd
efghZZ7ij@klm
noCODpqr
stuvw27z@xyz

如前所述,如果用於分割字符串的模式位於開頭(即示例代碼中的“ ZZ78”),則返回的第一個元素將為空字符串。 為避免這種情況,您所需要做的就是過濾數組。 這本質上與放置if相同,但是您可以通過這種方式避免多余的條件行。 我將按照以下步驟進行操作(在Java 8中):

String test_str = ...; // whatever string you want to test it with
Arrays.stream(str.split("ZZ78")).filter(s -> !s.isEmpty()).foreach(System.out::println);

由於[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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM