集册 Java实例教程 将字符串与模式匹配

将字符串与模式匹配

欢马劈雪     最近更新时间:2020-01-02 10:19:05

631
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
将字符串与模式匹配
public class Main {

  public static void main(String[] args) {

    // Prepare a regular expression to represent a pattern
    /* from 
    N  o w  J a v a . c o m*/

    String regex = ".@.";

                

                // Try matching many strings against the regular expression

    Main.matchIt("a@k", regex);

    Main.matchIt("webmaster@mypkg.com", regex);

    Main.matchIt("r@j", regex);

    Main.matchIt("a%N", regex);

    Main.matchIt(".@.", regex);

  }


  public static void matchIt(String str, String regex) {

    // Test for pattern match
    /** from 
    n o w j a   v  a . c o m - 时  代  Java**/

    if (str.matches(regex)) {

      System.out.println(str + 
展开阅读全文