美文网首页
正则表达式

正则表达式

作者: 逍遥_yjz | 来源:发表于2019-01-15 11:38 被阅读0次
    package regularExpression;
    
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class SingleTest {
        public static void main(String[] args) {
            String content = "I am noob " +
                    "from runoob.com.";
    
            String pattern = ".*runoob.*";
    
            boolean isMatch = Pattern.matches(pattern, content);
            System.out.println(Pattern.matches(pattern, content));
            System.out.println("字符串中是否包含了 'runoob' 子字符串? " + isMatch);
            System.out.println("================matches1111111111========================");
    
            System.out.println("测试字符串匹配问题");
            String recordReply1 = "我对着东西不喜欢,有什么感兴趣呐";
            String recordReply2 = "我对着东西不怎么感兴趣";
            String recordReply3 = "我不在,好===================================非常感兴趣呐";
            // 否定
            String recordReply4 = "我很不感兴趣";
            String recordReply5 = "我很感兴趣,但是不怎么喜欢";
    
            String word1 = ".*不.+感兴趣.*";
    
            System.out.println(Pattern.matches(word1, recordReply4));
    
            System.out.println("================matches2222222222========================");
            String recordReply6 = "你看看,不知道什么时候发工资,没有钱来用作消费吧呀";
            // 否定
            String recordReply7 = "我很不怎么感兴趣";
    
            String word2 = ".*看看.+什么时候.+有钱.+用.+吧.*";
            System.out.println(Pattern.matches(word2, recordReply6));
    
            System.out.println(recordReply7.getClass().toString());
    
            System.out.println("================replaceAll==11111========================");
            String REGEX = "\\*";
            String INPUT = "看看*什么时候*有钱*用*吧";
            String REPLACE = ".+";
            Pattern p = Pattern.compile(REGEX);
            // get a matcher object
            Matcher m = p.matcher(INPUT);
            INPUT = m.replaceAll(REPLACE);
            System.out.println(INPUT);
    
            //拼接字符串
            StringBuilder s5 = new StringBuilder(".*");
    
            s5.append(INPUT);
            s5.append(".*");
            System.out.println(s5.getClass().toString());
            System.out.println(s5.toString());
    
            String word3 = s5.toString();
    
            String recordReply8 = "你不看,不知道什么时候发工资,没有钱来用作消费吧呀看看";
            System.out.println(Pattern.matches(word3, recordReply8));
    
    
            System.out.println("=======================测试下划线的字符串====================");
            String shujuku = "_bu_*_gan_xing_qu_";
            //String shujuku = "_bu_gan_xing_qu_";  // 完全匹配 此举证明替换失败也没问题
            String strVoice1 = "_wo_dui_zhuo_dong_xi_bu_zen_me_gan_xing_qu_";
            System.out.println(Pattern.matches(shujuku, strVoice1));
            //处理替换字符串的
            String old_str = "\\*";
            String keyWord = shujuku;
            String new_str = ".+";
            Pattern p2 = Pattern.compile(old_str);
            // get a matcher object
            Matcher m2 = p2.matcher(keyWord);
            keyWord = m2.replaceAll(new_str);
            System.out.println(keyWord);
            // 字符串两边加*
            StringBuilder s6 = new StringBuilder(".*");
            s6.append(keyWord);
            s6.append(".*");
            keyWord = s6.toString();
            System.out.println(keyWord);
            // 匹配
            String strVoice2 = "_wo_dui_zhuo_dong_xi_bu_gan_xing_qu_";  //不会识别的
            String strVoice3 = "_wo_dui_zhuo_dong_xi_gan_xing_qu_";
            boolean isMatch2 =  Pattern.matches(keyWord, strVoice2);
            System.out.println("字符串中是否包含了 '不感兴趣的' 子字符串? " + isMatch2);
            if(isMatch2){
                System.out.println("匹配成功");
            }else {
                System.out.println("匹配失败");
            }
    
    
        }
    }
    
    

    结果输出:

    true
    字符串中是否包含了 'runoob' 子字符串? true
    ================matches1111111111========================
    测试字符串匹配问题
    false
    ================matches2222222222========================
    true
    class java.lang.String
    ================replaceAll==11111========================
    看看.+什么时候.+有钱.+用.+吧
    class java.lang.StringBuilder
    .*看看.+什么时候.+有钱.+用.+吧.*
    false
    =======================测试下划线的字符串====================
    false
    _bu_.+_gan_xing_qu_
    .*_bu_.+_gan_xing_qu_.*
    字符串中是否包含了 '不感兴趣的' 子字符串? false
    匹配失败
    

    相关文章

      网友评论

          本文标题:正则表达式

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