`

java正则表达式须知

阅读更多
1.正则表达式中的反斜杠用的是"\\",如果双反斜杠则是"\\\\"
  如要匹配单词:
  
   String result = "this is just a test";
   Pattern expression = Pattern.compile("\\w+");//定义正则表达式匹配单词
   Matcher matcher = expression.matcher(result);
   while(matcher.find()){
      String word = matcher.group();
    }
   

  匹配反斜杠:
  
  String testStr = "v2.suserStateId=\\u6B63\\u5F0F.replace(/"/g, '\"');v2.suserValidity=\"\\u6709\\u6548\".replace(/"/g, '\"')v2.suserStateId=\\u6B63\\u5F0F.replace(/"/g, '\"');";
        Pattern expression = Pattern.compile("\\\\u6B63\\\\u5F0F");//定义正则表达式匹配反斜杠,即需要匹配字符串'\u6709\u6548'
        Matcher matcher = expression.matcher(testStr);
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
   

  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics