使用正则表达式在字符串中搜索子字符串

我正在尝试搜索包含在ArrayList(terms_1pers)一个字符串中的一组单词,并且由于前提是搜索单词前后不应有字母,我想到了使用正则表达式。


我只是不知道我在使用匹配运算符时做错了什么。在报告的代码中,如果未验证匹配,则写入外部文件。


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");

}


MMMHUHU
浏览 166回答 3
3回答

幕布斯6054654

为了找到正则表达式匹配项,您应该使用正则表达式类。模式和匹配器。String term = "term";ArrayList<String> a&nbsp; = new ArrayList<String>();a.add("123term456"); //truea.add("A123Term5"); //falsea.add("term456"); //truea.add("123term"); //truePattern p = Pattern.compile("^[^A-Za-z]*(" + term + ")[^A-Za-z]*$");for(String text : a) {&nbsp; &nbsp; Matcher m = p.matcher(text);&nbsp; &nbsp; if (m.find()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Found: " + m.group(1) );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//since the term you are adding is the second matchable portion, you're looking for group(1)&nbsp; &nbsp; }&nbsp; &nbsp; 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&nbsp; = 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) {&nbsp; &nbsp; Pattern p = Pattern.compile("^[^A-Za-z]*(" + term + ")[^A-Za-z]*$");&nbsp; &nbsp; for(String text : a) {&nbsp; &nbsp; &nbsp; &nbsp; Matcher m = p.matcher(text);&nbsp; &nbsp; &nbsp; &nbsp; if (m.find()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Found: " + m.group(1)&nbsp; + " in " + text);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//since the term you are adding is the second matchable portion, you're looking for group(1)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else System.out.println("No match for: " + term + " in " + text);&nbsp; &nbsp; }}此输出为:找到:123term456 中的术语不匹配:A123Term5 中的术语不匹配:书中的术语在 table456 上....为了回答有关让字符串项不区分大小写的问题,这里有一种方法可以通过利用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++) {&nbsp; char c = term.charAt(i);&nbsp; if (Character.isLetter(c))&nbsp; &nbsp; str.append("(" + Character.toLowerCase(c) + "|" + Character.toUpperCase(c) + ")");&nbsp; 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!");此代码输出两行,第一行是在模式中编译的正则表达式字符串。"^[^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]*$"这个调整后的正则表达式允许匹配术语中的字母而不考虑大小写。第二行输出是“Found!” 因为混合大小写术语在 matchText 中找到。

白板的微信

有几点需要注意:matches需要完整的字符串匹配,因此[^a-z]term[^a-z]只会匹配像:term..&nbsp;您需要使用.find()来查找部分匹配项如果将文字字符串传递给正则表达式,则需要Pattern.quote它,或者如果它包含特殊字符,则不会匹配要检查单词之前或之后或在开始/结束时是否有某种模式,您应该使用带有锚点的交替(如(?:^|[^a-z])or&nbsp;(?:$|[^a-z]))或 lookarounds(?<![a-z])和(?![a-z])。要匹配任何字母,只需使用\p{Alpha}or - 如果您打算匹配任何 Unicode 字母 -&nbsp;\p{L}。该var变量设置为布尔类型更符合逻辑。固定代码:String url = csvRecord.get("url");String text = csvRecord.get("review");Boolean var = false;for(String term : terms_1pers){&nbsp; &nbsp;Matcher m = Pattern.compile("(?<!\\p{L})" + Pattern.quote(term) + "(?!\\p{L})").matcher(text);&nbsp; &nbsp;// If the search must be case insensitive use&nbsp; &nbsp;// Matcher m = Pattern.compile("(?i)(?<!\\p{L})" + Pattern.quote(term) + "(?!\\p{L})").matcher(text);&nbsp;&nbsp; &nbsp;if(!m.find())&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp;var = true;&nbsp; &nbsp;}}if (!var) {&nbsp; &nbsp;bw.write(url+";"+text+"\n");}

慕田峪4524236

您没有考虑开头和结尾可能包含字母的情况,因此在开头和结尾添加 .* 应该可以解决您的问题。for(String term : terms_1pers){&nbsp; &nbsp;if( text.matches(".*[^a-zA-Z]+" + term + "[^a-zA-Z]+.*)" )&nbsp;&nbsp;&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; var="true";&nbsp; &nbsp; &nbsp; break; //exit the loop&nbsp; &nbsp;}}if(!var.equals("true")){&nbsp; &nbsp; bw.write(url+";"+text+"\n");}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java