Java源码-使用正则表达式查询匹配记录

本例中的几个类和方法都是第一次学习,特此mark。


代码如下:

<pre class="java" name="code">//Fig. 14.24: RegexMatches.java
//Classes Pattern and Matcher.
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexMatches
{
public static void main(String[] args)
{
   // create regular expression
   Pattern expression = 
      Pattern.compile("H.*\\d[0-35-9]-\\d\\d-\\d\\d");
   
   String string1 = "Jane's Birthday is 05-12-75\n" +
      "Dave's Birthday is 11-04-68\n" +
      "John's Birthday is 04-28-73\n" +
      "Joe's Birthday is 12-17-77\n"+
      "H's Birthday is 80-01-01\n"+
      "h's Birthday is 80-10-20\n";

   // match regular expression to string and print matches
   Matcher matcher = expression.matcher(string1);
     
   while (matcher.find())
      System.out.println(matcher.group());
} 
} // end class RegexMatches

 
 


运行结果:

H's Birthday is 80-01-01




你可能感兴趣的:(java,源码,基础,正则表达式,core,jhtp)