开发者

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

开发者 https://www.devze.com 2023-01-01 21:27 出处:网络
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

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

精彩评论

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

关注公众号