码迷,mamicode.com
首页 > 编程语言 > 详细

java正则表达式取出匹配字符串

时间:2019-04-25 16:04:28      阅读:684      评论:0      收藏:0      [点我收藏+]

标签:表达   sys   main   lin   pre   static   else   查找   字符串   

 

 

import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class RegexMatches
{
    public static void main( String args[] ){
 
      // 按指定模式在字符串查找
      String line = "This order was placed for QT3000! OK?";
      String pattern = "(\\D*)(\\d+)(.*)";
 
      // 创建 Pattern 对象
      Pattern r = Pattern.compile(pattern);
 
      // 现在创建 matcher 对象
      Matcher m = r.matcher(line);
      if (m.find( )) {
         System.out.println("Found value: " + m.group(0) );
         System.out.println("Found value: " + m.group(1) );
         System.out.println("Found value: " + m.group(2) );
         System.out.println("Found value: " + m.group(3) ); 
      } else {
         System.out.println("NO MATCH");
      }
   }
}

 

运行结果:

Found value: This order was placed for QT3000! OK?
Found value: This order was placed for QT
Found value: 3000
Found value: ! OK?

 

java正则表达式取出匹配字符串

标签:表达   sys   main   lin   pre   static   else   查找   字符串   

原文地址:https://www.cnblogs.com/libin6505/p/10768831.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!