java.regex包的模式类是正则表达式的编译表示。

此类的compile()方法接受表示正则表达式的字符串值,并返回Pattern对象。

示例import java.util.Scanner;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class CompileExample {

public static void main( String args[] ) {

//读取字符串值

Scanner sc = new Scanner(System.in);

System.out.println("Enter input string");

String input = sc.nextLine();

//查找数字的正则表达式

String regex = "(\\d)";

//编译正则表达式

Pattern pattern = Pattern.compile(regex);

//打印正则表达式

System.out.println("Compiled regular expression: "+pattern.toString());

//检索匹配器对象

Matcher matcher = pattern.matcher(input);

//验证是否发生匹配

if(matcher.find()) {

System.out.println("Given String contains digits");

} else {

System.out.println("Given String does not contain digits");

}

}

}

输出结果Enter input string

hello my id is 1120KKA

Compiled regular expression: (\d)

Given String contains digits

此方法的另一个变体接受表示标志的整数值,其中每个标志指定一个可选条件,例如CASE_INSENSITIVE在编译正则表达式时忽略大小写。

示例import java.util.Scanner;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class CompileExample {

public static void main( String args[] ) {

//编译正则表达式

Pattern pattern = Pattern.compile("[t]", Pattern.CASE_INSENSITIVE);

//检索匹配器对象

Matcher matcher = pattern.matcher("Nhooo");

int count = 0;

while(matcher.find()) {

count++;

}

System.out.println("Number of matches: "+count);

}

}

输出结果Enter input string

Nhooo

Number of matches: 3

java compile方法_Java中带有示例的模式compile()方法相关推荐

  1. ruby hash方法_Ruby中带有示例的Hash.select方法

    ruby hash方法 哈希选择方法 (Hash.select Method) In this article, we will study about Hash.select Method. The ...

  2. ruby hash方法_Ruby中带有示例的Hash.rehash方法

    ruby hash方法 Hash.rehash方法 (Hash.rehash Method) In this article, we will study about Hash.rehash Meth ...

  3. ruby hash方法_Ruby中带有示例的Hash.invert方法

    ruby hash方法 Hash.invert方法 (Hash.invert Method) In this article, we will study about Hash.invert Meth ...

  4. ruby hash方法_Ruby中带有示例的Hash.flatten方法

    ruby hash方法 哈希平化方法 (Hash.flatten Method) In this article, we will study about Hash.flatten Method. T ...

  5. ruby hash方法_Ruby中带有示例的Hash.values方法

    ruby hash方法 哈希值方法 (Hash.values Method) In this article, we will study about Hash.values Method. The ...

  6. ruby hash方法_Ruby中带有示例的Hash.length方法

    ruby hash方法 哈希长度方法 (Hash.length Method) In this article, we will study about Hash.length Method. The ...

  7. ruby hash方法_Ruby中带有示例的Hash.keys方法

    ruby hash方法 哈希键方法 (Hash.keys Method) In this article, we will study about Hash.keys Method. The work ...

  8. java的flush方法_Java中的BufferedWriter flush()方法及示例

    Java中buffered writer类的flush()方法用于刷新缓冲的writer流中的字符.语法:公共void flush()指定者:此方法由Flushable接口的flush()方法指定.重 ...

  9. java 不定参数方法_java中不定长参数的使用方法

    java中不定长参数的使用方法 不定长参数方法的语法如下:返回值 方法名(参数类型...参数名称) 在参数列表中使用"..."形式定义不定长参数,其实这个不定长参数a就是一个数组, ...

  10. java多线程同步的四种方法_java中实现多线程的两种方法

    java多线程有几种实现方法,都是什么?同步有几种实java中多线程的实现方法有两种:1.直接继承thread类:2.实现runnable接口:同步的实现方法有五种:1.同步方法:2.同步代码块:3. ...

最新文章

  1. Redis开发:hash存储自定义Java对象及value的序列化器设置
  2. mysql使用索引为什么查询速度变快很多?
  3. 从一个实例看jaxb的强大
  4. Winsock网络编程快速入门
  5. Vue 实现前后端分离项目
  6. Java泛型的PECS原则
  7. 用nohup执行python程序时,print无法输出
  8. Linux虚拟机之间实现密钥登陆
  9. 南充高中计算机老师,2021四川南充教师招聘考试高中信息技术说课稿之《表格的装饰》...
  10. Python编写工资计算器
  11. Java代码实现幸运抽奖
  12. matlab有限差分一维导热,一维导热方程-有限差分法-matlab实现.docx
  13. Air202入坑指南1---lua开发环境的安装
  14. 【CTS】Ubuntu下安装CTS测试环境
  15. week9 day1 HTML简介和常用标签
  16. lwip连续发数据卡死_用lwip发送大量数据时,遇到的问题解答记录;
  17. 手机页面点击电话进入拨号界面---a链接的tel属性
  18. 消防工程师 8.3 防排烟系统-排烟
  19. win10查看计算机系统版本,Win10版本怎么看?Win10系统版本查看方法
  20. stc89c52 单片机 修改头文件

热门文章

  1. golang 实现 syn_sent flood洪水攻击
  2. HTTPS的原理与具体实现过程及HTTP服务的虚拟目录简介
  3. 软件需求分析期末试题
  4. 20200404 时间飞逝 青春不在
  5. 微信开发者工具模拟扫描二维码调试
  6. 南京信息工程大学计算机与科学专业,南京信息工程大学王牌专业有哪些及专业排名...
  7. 【PAT乙级】B1001-B1095刷题记录
  8. 问题解决:下载的网页打开后自动跳转到首页
  9. 用border-color 的 transparent 制作一个三角形
  10. 仿支付宝实现密码框输入