Guava:CharMatcher字符匹配器

发布时间:2024年01月11日

简介

CharMatcher 提供了各种方法来处理各种 JAVA char 类型值。

类方法说明

官方文档:CharMatcher (Guava: Google Core Libraries for Java 27.0.1-jre API)

方法名称方法说明
and(CharMatcher other)?修饰匹配器,返回当前匹配器与other匹配器做与操作的匹配器.
any()?匹配任意字符的匹配器.
anyOf(CharSequence sequence)?通过sequence初始化匹配器,该匹配器可以匹配sequence中所有字符.
ascii()?获取可以匹配所有ascii码的匹配器.
breakingWhitespace()?获取可以匹配所有可换行的空白字符的匹配器(不包括非换行空白字符,例如”\u00a0″).
collapseFrom(CharSequence sequence, char replacement)?折叠操作,将charMatcher连续被匹配到的字符用一个replacement替换.
countIn(CharSequence sequence)?获取charMatcher在sequence中匹配到字符的个数.
forPredicate(Predicate<??super?Character>?predicate)?
通过Predicate初始化CharMatcher,该匹配器可以匹配Predicate函数式接口apply方法实现返回True的字符.
indexIn(CharSequence sequence)?获取charMatcher在当sequence中匹配到的第一个字符的index.
indexIn(CharSequence sequence, int start)?获取charMatcher在当sequence中从index start开始匹配到的第一个字符的index.
inRange(char startInclusive, char endInclusive)?通过边界值初始化CharMatcher,该匹配器可以匹配处于startInclusive和endInclusive之间的所有字符.
is(char match)?
通过单个字符初始化CharMatcher,该匹配器只能匹配match这个单字符.
isNot(char match)?通过单个字符初始化CharMatcher,该匹配器可以匹配除了match之外的所有字符.
javaIsoControl()?
获取可以匹配所有Java转义字符的匹配器.
lastIndexIn(CharSequence sequence)?获取获取charMatcher在当sequence中匹配到的最后一个字符的index.
matches(char c)?确定给定字符的true或false值。
matchesAllOf(CharSequence sequence)?判断sequence所有字符是否都被charMatcher匹配.
matchesAnyOf(CharSequence sequence)?
判断sequence中是否存在字符被charMatcher匹配.
matchesNoneOf(CharSequence sequence)?判断sequence所有字符是否都没被charMatcher匹配.
negate()?修饰匹配器,返回和当前匹配器相反的匹配器.
none()?不匹配任何字符的匹配器,与any()相反.
noneOf(CharSequence sequence)?
通过sequence初始化匹配器,该匹配器可以匹配除sequence之外的所有字符.
or(CharMatcher other)?修饰匹配器,返回当前匹配器与other匹配器做或操作的匹配器.
precomputed()?修饰匹配器,返回的CharMatcher在检索时比原始的CharMatcher效率高,但是预处理也需要花时间,所以只有当某个 CharMatcher需要被使用上千次的时候才有必要进行预处理.
removeFrom(CharSequence sequence)?删除sequence中所有被charMatcher匹配到的字符.
replaceFrom(CharSequence sequence, char replacement)?将sequence中所有被charMatcher匹配到的字符用replacement替换.
replaceFrom(CharSequence sequence, CharSequence replacement)?将sequence中所有被charMatcher匹配到的字符用replacement替换.
retainFrom(CharSequence sequence)?
保留sequence中所有被charMatcher匹配到的字符.
toString()?返回此CharMatcher的字符串表示形式,例如CharMatcher.or(WHITESPACE, JAVA_DIGIT).
trimAndCollapseFrom(CharSequence sequence, char replacement)?先对sequence做trim操作(删除sequence头和尾的空格),再对trim的结果做collapse操作(将charMatcher连续被匹配到的字符用一个replacement替换).
trimFrom(CharSequence sequence)?删除sequence首尾charMatcher匹配到的字符.
trimLeadingFrom(CharSequence sequence)?
删除sequence首部charMatcher匹配到的字符.
StrimTrailingFrom(CharSequence sequence)?删除sequence尾部charMatcher匹配到的字符.
whitespace()?
获取可以匹配所有空格的匹配器.

使用Demo

import com.google.common.base.CharMatcher;
import org.junit.Test;

/**
 * 字符匹配
 */
public class CharMatcherTest {

    /**
     * collapseFrom 配到的字符做替换
     */
    @Test
    public void testCollapseFrom() {
        //配到的字符做替换
        //匹配空白
        String input = "     Hello World        ";
        String result = CharMatcher.breakingWhitespace().collapseFrom(input, '*');
        System.out.println(result);//*Hello*World*
        //匹配指定字符
        String result1 = CharMatcher.is('l').collapseFrom(input, '*');
        System.out.println(result1);//     He*o Wor*d    注意:两边空格还在

        //匹配指定字符
        String result2 = CharMatcher.inRange('l','o').collapseFrom(input, '*');
        System.out.println(result2);//     He* W*r*d    注意:两边空格还在

        //trimAndCollapseFrom:先去掉两边空格再进行替换
        String result3 = CharMatcher.breakingWhitespace().trimAndCollapseFrom(input, '*');
        System.out.println(result3);//Hello*World
    }

    /**
     * 去除空格
     */
    @Test
    public void testTrim() {
        String str = "     Hello World     ";

        //trimFrom 去两边空格
        System.out.println(CharMatcher.breakingWhitespace().trimFrom(str));           // Hello World
        //trimLeadingFrom 左边去空格
        System.out.println(CharMatcher.breakingWhitespace().trimLeadingFrom(str));    // Hello World
        //trimTrailingFrom右边去空格
        System.out.println(CharMatcher.breakingWhitespace().trimTrailingFrom(str));   //      Hello World
        System.out.println(CharMatcher.breakingWhitespace().removeFrom(str));   //HelloWorld
    }

    /**
     * retainFrom 保留匹配到的字符
     */
    @Test
    public void testRetainFrom() {
        String str = "   Hi 123 Hello 456 World  ";
        System.out.println(CharMatcher.is('l').retainFrom(str));   //lll

        //匹配字母和数字
        CharMatcher matcher = CharMatcher.forPredicate(Character::isLetterOrDigit);//Hi123Hello456World
        System.out.println(matcher.retainFrom(str));

        //匹配小写
        CharMatcher matcher2 = CharMatcher.forPredicate(Character::isLowerCase);//ielloorld
        System.out.println(matcher2.retainFrom(str));
    }

    /**
     * removeFrom 删除所有匹配的字符
     */
    @Test
    public void testRemoveFrom() {
        System.out.println(CharMatcher.is('l').removeFrom("   Hi 123 Hello 456 World  "));   //   Hi 123 Heo 456 Word
    }

    /**
     * countIn 查找字符在字符串中的个数
     */
    @Test
    public void testCountIn() {
        System.out.println(CharMatcher.is('a').countIn("Hello World Sharing the Google Guava Used"));  // 3

        String input = "H*el.lo,}12";
        //匹配数子
        CharMatcher matcher = CharMatcher.forPredicate(Character::isDigit);
        System.out.println(matcher.countIn(input));     //2

        matcher = CharMatcher.inRange('a', 'l');
        System.out.println(matcher.countIn(input));     // 3
    }

    /**
     * indexIn 匹配到的第一个字符的index
     * lastIndexIn 匹配到的最后一个字符的index
     *
     * 下标从0开始 若不存在返回-1
     */
    @Test
    public void testIndexIn_lastIndexIn() {
        String input = "Hello World";
        CharMatcher matcher = CharMatcher.is('l');

        System.out.println(matcher.indexIn(input));//2
        System.out.println(matcher.indexIn(input, 10));//-1
        System.out.println(matcher.lastIndexIn(input));//9
    }

    //=======匹配===========//

    /**
     * is 匹配参数之内的所有字符
     * isNot 匹配参数之外的所有字符
     */
    @Test
    public void testIs_isNot(){
        String input = "a, c, z, 1, 2";
        System.out.println(CharMatcher.is(',').retainFrom(input));   // ,,,,
        System.out.println(CharMatcher.is(',').removeFrom(input));   // a c z 1 2

        System.out.println(CharMatcher.isNot(',').retainFrom(input));   // a c z 1 2
        System.out.println(CharMatcher.isNot(',').removeFrom(input));   // ,,,,
    }

    /**
     * 匹配java转义字符
     */
    @Test
    public void testJavaIsoControl(){
        String input = "ab\tcd\nef\bg";
        CharMatcher matcher = CharMatcher.javaIsoControl();
        System.out.println(matcher.removeFrom(input));  // abcdefg
    }

    /**
     * 两个 Matcher 同时匹配
     */
    @Test
    public void testDoubleMatcher() {
        CharMatcher matcher0 = CharMatcher.forPredicate(Character::isLetterOrDigit);
        CharMatcher matcher1 = CharMatcher.forPredicate(Character::isLowerCase);

        String result = matcher0.and(matcher1).retainFrom("H*el.lo,}12");
        System.out.println(result); // ell0
    }

    /**
     * matchesAllOf 判断sequence所有字符是否都被charMatcher匹配
     * matchesAnyOf 判断sequence中是否存在字符被charMatcher匹配
     * matchesNoneOf 判断sequence所有字符是否都没被charMatcher匹配
     */
    @Test
    public void test_matchesAllOf_matchesAnyOf_matchesNoneOf(){
        String input = "**e,l.lo,}12";

        CharMatcher matcher = CharMatcher.is(',');
        System.out.println(matcher.matchesAllOf(input));    // false

        matcher = CharMatcher.is(',');
        System.out.println(matcher.matchesAnyOf(input));    // true

        matcher = CharMatcher.is('?');
        System.out.println(matcher.matchesNoneOf(input));   // true
    }

    /**
     * 匹配任意字符
     */
    @Test
    public void testAny() {
        String input = "H*el.lo,}12";

        CharMatcher matcher = CharMatcher.any();
        String result = matcher.replaceFrom(input,"替换,");
        System.out.println(result); //替换,替换,替换,替换,替换,替换,替换,替换,替换,替换,替换,

        matcher = CharMatcher.anyOf("Hel");
        System.out.println(matcher.retainFrom(input)); // Hell
        System.out.println(matcher.removeFrom(input)); // *.o,}12
    }

    /**
     * 匹配 Ascii 常用的字符编码标准
     */
    @Test
    public void testAscii() {
        String input = "罗あH*el.lo,}12";

        CharMatcher matcher = CharMatcher.ascii();
        System.out.println(matcher.retainFrom(input));  // H*el.lo,}12
        System.out.println(matcher.removeFrom(input));  // 罗あ
    }

    /**
     * 取反
     * negate 返回与当前CharMatcher相反的CharMatcher
     */
    @Test
    public void testNegate(){
        String input = "あH*el.lo,}12";

        CharMatcher matcher = CharMatcher.ascii().negate();
        System.out.println(matcher.retainFrom(input));     //  あ
        System.out.println(matcher.removeFrom(input));     //  H*el.lo,}12
    }

    /**
     * none 不匹配任何字符,与any()相反
     * noneOf 不匹配CharSequence内的任意一个字符,与anyOf()相反
     */
    @Test
    public void testNone_noneOf(){
        String input = "H*el.lo,}12";
        CharMatcher matcher = CharMatcher.none();
        System.out.println(matcher.retainFrom(input));          // ""
        System.out.println(matcher.retainFrom(input).length()); // 0

        matcher = CharMatcher.noneOf("Hel");
        System.out.println(matcher.retainFrom(input));          // *.o,}12
        System.out.println(matcher.removeFrom(input));          // Hell
    }

    /**
     * forPredicate 初始化匹配器
     */
    public void testForPredicate() {
//        CharMatcher charMatcher = CharMatcher.forPredicate(new Predicate<Character>() {
//            @Override
//            public boolean apply(@Nullable Character input) {
//                return Character.isLetterOrDigit(input);
//            }
//        });

        // lambda 写法
        CharMatcher charMatcher = CharMatcher.forPredicate(input -> Character.isLetterOrDigit(input));

        String input = "H*el.lo,}12";
        System.out.println(charMatcher.retainFrom(input));  // Hello12

    }
}
文章来源:https://blog.csdn.net/qq_63815371/article/details/135485685
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。