日期:2014-05-20  浏览次数:20663 次

如何实现字符串的匹配
目前要在一个大的字符串中对给定的字符串进行匹配,但是这种匹配应该是以单词为单位进行的,比如说,
要查找的字符串是:Life likes using google, you need to know what you are searching for, ing.
要进行匹配的字串是:ing
如果按照一般的匹配方式的话,using, searching和ing都会被匹配到,但是现在要以整个单词为单位,所以using和searching是不能匹配出来的,只有ing能被匹配到。

希望大家能指导一下,给点思路给鄙人。谢谢了!

------解决方案--------------------
先把每个单词单独拿出来,然后看看每个单词是否含有ing,如果含有就是需要找的单词!
------解决方案--------------------
Java code

public class Test25 {

    public static void main(String[] arg) {

        String str = "Life likes using google, you need to know what you are searching for, ing.";
        String[] strs = str.split("(?i)[^a-zA-Z0-9\u4E00-\u9FA5]");
        for (String s : strs) {
            if (s.contains("ing"))
                System.out.println(s);
        }
    }
}

------解决方案--------------------
Pattern p = Pattern.compile("(([a-zA-Z]*)?ing)\\b");
String str = "Life likes using google, you need to know what you are searching for, ing.";
Matcher m = p.matcher(str);
while(m.find()){
System.out.println(m.group());
}