由于工作中用到,之前没有用过,故在此记录一下使用方法。

  java提供了一个简单好用的字符串匹配的类。为了方便调试,我使用SpannableString给关键字换了颜色。直接上代码

        testBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String keyStr = inputEdit.getText().toString();
                String contentStr = "ABAAAAAaaaaaaaaAAAAAaaaaAaAAAAaadfjaldf";
                Pattern pattern = Pattern.compile(keyStr,Pattern.CASE_INSENSITIVE);
                Matcher matcher = pattern.matcher(contentStr);
                SpannableString span = new SpannableString(contentStr);
                while (matcher.find()){
                    span.setSpan(new ForegroundColorSpan(Color.RED) , matcher.start() , matcher.end() , Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                }
                outPutTextView.setText(span);
            }
        });

  大致过程就是先建一个pattern,把要匹配的关键词——keyStr放进去,并选择模式。然后,从pattern中得到matcher,同时输入要被搜索的内容——contentStr。然后使用while语句,循环查找。每次查找到,都可以通过matcher.start()和matcher.end()来获取关键词的位置。

  其中,每种模式的功能,源码都有注释。文中是用的字母忽略大小写的模式,非常实用!!!

    /**
     * This constant specifies that a pattern matches Unix line endings ('\n')
     * only against the '.', '^', and '$' meta characters. Corresponds to {@code (?d)}.
     */
    public static final int UNIX_LINES = 0x01;

    /**
     * This constant specifies that a {@code Pattern} is matched
     * case-insensitively. That is, the patterns "a+" and "A+" would both match
     * the string "aAaAaA". See {@link #UNICODE_CASE}. Corresponds to {@code (?i)}.
     */
    public static final int CASE_INSENSITIVE = 0x02;

    /**
     * This constant specifies that a {@code Pattern} may contain whitespace or
     * comments. Otherwise comments and whitespace are taken as literal
     * characters. Corresponds to {@code (?x)}.
     */
    public static final int COMMENTS = 0x04;

    /**
     * This constant specifies that the meta characters '^' and '$' match only
     * the beginning and end of an input line, respectively. Normally, they
     * match the beginning and the end of the complete input. Corresponds to {@code (?m)}.
     */
    public static final int MULTILINE = 0x08;

    /**
     * This constant specifies that the whole {@code Pattern} is to be taken
     * literally, that is, all meta characters lose their meanings.
     */
    public static final int LITERAL = 0x10;

    /**
     * This constant specifies that the '.' meta character matches arbitrary
     * characters, including line endings, which is normally not the case.
     * Corresponds to {@code (?s)}.
     */
    public static final int DOTALL = 0x20;

    /**
     * This constant specifies that a {@code Pattern} that uses case-insensitive matching
     * will use Unicode case folding. On Android, {@code UNICODE_CASE} is always on:
     * case-insensitive matching will always be Unicode-aware. If your code is intended to
     * be portable and uses case-insensitive matching on non-ASCII characters, you should
     * use this flag. Corresponds to {@code (?u)}.
     */
    public static final int UNICODE_CASE = 0x40;

    /**
     * This constant specifies that a character in a {@code Pattern} and a
     * character in the input string only match if they are canonically
     * equivalent. It is (currently) not supported in Android.
     */
    public static final int CANON_EQ = 0x80;

Done.简记于此。

posted on 2015-06-05 00:20  Fishbonell  阅读(203)  评论(0编辑  收藏  举报