开发者

Are there any way to apply regexp in java ignoring letter case?

Simple example: we have string "Some sample string Of Text". And I want to filter out all stop words (i.e. "som开发者_开发百科e" and "of") but I don't want to change letter case of other words which should be retained.

If letter case was unimportant I would do this:

str.toLowerCase().replaceAll ("a|the|of|some|any", "");

Is there an "ignore case" solution with regular expressions in java?


You can use the inline case-insensitive modifier:

str.replaceAll ("(?i)a|the|of|some|any", "");


Something like this should do the trick as well:

        Pattern pat = Pattern.compile("a|the|of|some|any", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pat.matcher(str);
        String result = matcher.replaceAll("");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜