|
问题:str.matches("word");没有效果
原因:函数matches()对整个字符串做匹配,不是找单个词
解决:改为str.match(".*\\bword\\b.*);
参考:
/** * Demonstrates how to use the String.matches() method, including * the need to match the entire string in your patterns. */public class StringMatches1{ public static void main(String[] args) { String stringToSearch = "Four score and seven years ago our fathers ..."; // this prints "false", because the search pattern doesn't match the // entire string System.out.println("Try 1: " + stringToSearch.matches("seven")); // this prints "true" because the pattern does match the entire string System.out.println("Try 2: " + stringToSearch.matches(".*seven.*")); }}输出:Try 1: falseTry 2: true |
|