美文网首页
正则匹配

正则匹配

作者: 十旋转45度 | 来源:发表于2018-07-25 17:18 被阅读0次

    Java正则表达式特殊字符及其处理以及正则表达式详解

    image.png
            // 清除掉所有特殊字符
            String regEx =  ".*[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?\\\\]+.*";
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(str);
            return m.matches();
        }
                                 
         public   static   String filterString(String   str)   throws   PatternSyntaxException   {  
             String regEx= "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?\\\\]";
             Pattern   p   =   Pattern.compile(regEx);  
             Matcher   m   =   p.matcher(str);  
             return   m.replaceAll("_").trim();  
         }
    

    打造原生的图文混排控件

     public void setContent(String content) {
        // 格式化字符串(替换特殊符号)
        String text = null;
        // 设置子View水平居中
        setGravity(Gravity.CENTER_HORIZONTAL);
        Pattern pattern = Pattern.compile(imageRegex);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
             // 加载文字
            text = content.substring(startPos, matcher.start());
            if (!TextUtils.isEmpty(text)) {
                appendTextView(clearNewlineChar(text));
            }
            // 加载图片
            appendImageView(content.substring(matcher.start() + 5, matcher.end() - 6));
            startPos = matcher.end();
        }
        // 加载最后一个图片后面的文字
        text = content.substring(startPos);
        if (!TextUtils.isEmpty(text)) {
            appendTextView(clearNewlineChar(text));
        }
    }
    

    相关文章

      网友评论

          本文标题:正则匹配

          本文链接:https://www.haomeiwen.com/subject/klcmmftx.html