java compile方法_Java中带有示例的模式compile()方法
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()方法相关推荐
- ruby hash方法_Ruby中带有示例的Hash.select方法
ruby hash方法 哈希选择方法 (Hash.select Method) In this article, we will study about Hash.select Method. The ...
- ruby hash方法_Ruby中带有示例的Hash.rehash方法
ruby hash方法 Hash.rehash方法 (Hash.rehash Method) In this article, we will study about Hash.rehash Meth ...
- ruby hash方法_Ruby中带有示例的Hash.invert方法
ruby hash方法 Hash.invert方法 (Hash.invert Method) In this article, we will study about Hash.invert Meth ...
- ruby hash方法_Ruby中带有示例的Hash.flatten方法
ruby hash方法 哈希平化方法 (Hash.flatten Method) In this article, we will study about Hash.flatten Method. T ...
- ruby hash方法_Ruby中带有示例的Hash.values方法
ruby hash方法 哈希值方法 (Hash.values Method) In this article, we will study about Hash.values Method. The ...
- ruby hash方法_Ruby中带有示例的Hash.length方法
ruby hash方法 哈希长度方法 (Hash.length Method) In this article, we will study about Hash.length Method. The ...
- ruby hash方法_Ruby中带有示例的Hash.keys方法
ruby hash方法 哈希键方法 (Hash.keys Method) In this article, we will study about Hash.keys Method. The work ...
- java的flush方法_Java中的BufferedWriter flush()方法及示例
Java中buffered writer类的flush()方法用于刷新缓冲的writer流中的字符.语法:公共void flush()指定者:此方法由Flushable接口的flush()方法指定.重 ...
- java 不定参数方法_java中不定长参数的使用方法
java中不定长参数的使用方法 不定长参数方法的语法如下:返回值 方法名(参数类型...参数名称) 在参数列表中使用"..."形式定义不定长参数,其实这个不定长参数a就是一个数组, ...
- java多线程同步的四种方法_java中实现多线程的两种方法
java多线程有几种实现方法,都是什么?同步有几种实java中多线程的实现方法有两种:1.直接继承thread类:2.实现runnable接口:同步的实现方法有五种:1.同步方法:2.同步代码块:3. ...
最新文章
- Redis开发:hash存储自定义Java对象及value的序列化器设置
- mysql使用索引为什么查询速度变快很多?
- 从一个实例看jaxb的强大
- Winsock网络编程快速入门
- Vue 实现前后端分离项目
- Java泛型的PECS原则
- 用nohup执行python程序时,print无法输出
- Linux虚拟机之间实现密钥登陆
- 南充高中计算机老师,2021四川南充教师招聘考试高中信息技术说课稿之《表格的装饰》...
- Python编写工资计算器
- Java代码实现幸运抽奖
- matlab有限差分一维导热,一维导热方程-有限差分法-matlab实现.docx
- Air202入坑指南1---lua开发环境的安装
- 【CTS】Ubuntu下安装CTS测试环境
- week9 day1 HTML简介和常用标签
- lwip连续发数据卡死_用lwip发送大量数据时,遇到的问题解答记录;
- 手机页面点击电话进入拨号界面---a链接的tel属性
- 消防工程师 8.3 防排烟系统-排烟
- win10查看计算机系统版本,Win10版本怎么看?Win10系统版本查看方法
- stc89c52 单片机 修改头文件