簡體   English   中英

使用正則表達式搜索字符串中的子字符串

[英]Search substring in a string using regex

我正在嘗試搜索字符串中包含在ArrayList(terms_1pers)內的一組單詞,並且由於前提是搜索單詞之前和之后不應有字母,因此我想到了使用正則表達式。

我只是不知道我使用Matchs運算符在做什么錯。 在報告的代碼中,如果未驗證匹配項,它將寫入外部文件。

String url = csvRecord.get("url");
String text = csvRecord.get("review");
String var = null;
for(String term : terms_1pers)
{
   if(!text.matches("[^a-z]"+term+"[^a-z]"))
   {
      var="true";
   }
}
if(!var.equals("true"))
{
    bw.write(url+";"+text+"\n");
}

為了找到正則表達式匹配項,您應該使用正則表達式類。 模式和匹配器。

String term = "term";
ArrayList<String> a  = new ArrayList<String>();
a.add("123term456"); //true
a.add("A123Term5"); //false
a.add("term456"); //true
a.add("123term"); //true
Pattern p = Pattern.compile("^[^A-Za-z]*(" + term + ")[^A-Za-z]*$");
for(String text : a) {
    Matcher m = p.matcher(text);
    if (m.find()) {
         System.out.println("Found: " + m.group(1) );
         //since the term you are adding is the second matchable portion, you're looking for group(1)
    }
    else System.out.println("No match for: " + term);
}

}

在該示例中,我們創建一個https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html的實例,以在您要匹配的文本中查找匹配項。

請注意,我對正則表達式做了一些調整。 此代碼中的選擇從初始匹配部分中排除所有字母AZ和小寫版本。 它還將允許在匹配項之前或之后根本沒有字符的情況。 如果需要在此處放置某些內容,請使用+代替* 我還通過使用^$來驗證正則表達式的結尾來限制正則表達式來強制匹配只包含這三個組的匹配。 如果這不適合您的用例,則可能需要進行調整。

為了以各種不同的術語演示使用此方法:

ArrayList<String> terms = new ArrayList<String>();
terms.add("term");
terms.add("the book is on the table");
terms.add("1981 was the best year ever!");
ArrayList<String> a  = new ArrayList<String>();
a.add("123term456");
a.add("A123Term5");
a.add("the book is on the table456");
a.add("1@#!231981 was the best year ever!9#");
for (String term: terms) {

    Pattern p = Pattern.compile("^[^A-Za-z]*(" + term + ")[^A-Za-z]*$");

    for(String text : a) {

        Matcher m = p.matcher(text);
        if (m.find()) {
             System.out.println("Found: " + m.group(1)  + " in " + text);
             //since the term you are adding is the second matchable portion, you're looking for group(1)
        }
        else System.out.println("No match for: " + term + " in " + text);
    }
}

其輸出為:找到:123term456中的術語匹配項:A123Term5中的術語不匹配:書中的術語在表456上。...

回答有關使String術語不區分大小寫的問題,這是一種我們可以通過利用java.lang.Character作為大小寫字母選項的方式來構建字符串的方法。

String term = "This iS the teRm.";
String matchText = "123This is the term.";
StringBuilder str = new StringBuilder();
str.append("^[^A-Za-z]*(");
for (int i = 0; i < term.length(); i++) {
  char c = term.charAt(i);
  if (Character.isLetter(c))
    str.append("(" + Character.toLowerCase(c) + "|" + Character.toUpperCase(c) + ")");
  else str.append(c);
}
str.append(")[^A-Za-z]*$");

System.out.println(str.toString());


Pattern p = Pattern.compile(str.toString());
Matcher m = p.matcher(matchText);
if (m.find()) System.out.println("Found!");
else System.out.println("Not Found!");

此代碼輸出兩行,第一行是正在Pattern中編譯的正則表達式字符串。 "^[^A-Za-z]*((t|T)(h|H)(i|I)(s|S) (i|I)(s|S) (t|T)(h|H)(e|E) (t|T)(e|E)(r|R)(m|M).)[^A-Za-z]*$"此調整后的正則表達式允許在無論大小寫都可以匹配。 第二個輸出行是“找到!” 因為在matchText中可以找到大小寫混合的字詞。

您沒有考慮開頭和結尾可能包含字母的情況,因此在開頭和結尾添加。*應該可以解決您的問題。

for(String term : terms_1pers)
{
   if( text.matches(".*[^a-zA-Z]+" + term + "[^a-zA-Z]+.*)" )  
   {
      var="true";
      break; //exit the loop
   }
}
if(!var.equals("true"))
{
    bw.write(url+";"+text+"\n");
}

有幾件事要注意:

  • matches需要完整的字符串匹配,因此[^az]term[^az]僅匹配:term.類的字符串:term. 您需要使用.find()查找部分匹配項
  • 如果將文字字符串傳遞給正則表達式,則需要使用Pattern.quote對其進行Pattern.quote ,或者如果它包含特殊字符,則不會匹配它
  • 要檢查單詞之前或之后在開始/結束有一定的模式,您應該使用交替用錨(比如(?:^|[^az])(?:$|[^az])或周圍環境(?<![az])(?![az])
  • 要匹配任何字母,請使用\\p{Alpha}或者-如果您打算匹配任何Unicode字母,請使用\\p{L}
  • var變量設置為布爾類型更具邏輯性。

固定代碼:

String url = csvRecord.get("url");
String text = csvRecord.get("review");
Boolean var = false;
for(String term : terms_1pers)
{
   Matcher m = Pattern.compile("(?<!\\p{L})" + Pattern.quote(term) + "(?!\\p{L})").matcher(text);
   // If the search must be case insensitive use
   // Matcher m = Pattern.compile("(?i)(?<!\\p{L})" + Pattern.quote(term) + "(?!\\p{L})").matcher(text); 
   if(!m.find())
   {
       var = true;
   }
}
if (!var) {
   bw.write(url+";"+text+"\n");
}

暫無
暫無

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

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