JAVA Pattern.matches的使用
1.首先阅读Pattern.matches的API:
boolean java.util.regex.Pattern.matches(String regex, CharSequence input)
Compiles the given regular expression and attempts to match the given input against it.
An invocation of this convenience method of the form
Pattern.matches(regex, input);
behaves in exactly the same way as the expression
Pattern.compile(regex).matcher(input).matches()
If a pattern is to be used multiple times, compiling it once and reusing it will be more efficient than invoking this method each time.
Parameters:regex The expression to be compiledinput The character sequence to be matchedReturns:whether or not the regular expression matches on the inputThrows
:PatternSyntaxException -
If the expression's syntax is invalid
什么意思,看不懂没关系,看下面的例子
String tx ="s_safasfasfqewgsvzvdsvgf.";boolean t = Pattern.matches("^[a-zA-Z]\\w*$", tx);System.out.println(t);
代码的意思就是 :字符串tx是不是匹配正则表达式^[a-zA-Z]\\w*$;,如果匹配就返回ture;如果不匹配就返回false。
知识点2:^[a-zA-Z]\\w*$
^表示正则表达式的开始;
$表示正常表达式的结尾;
[a-zA-z]表示任意一个英文字母,不区分大小写
\\w* 表示任意一个字符,字符类型为:数字,下划线和字母
结合起来就是:一个以字母开头的以,下划线和数组字母构成的字符串!
现在我们在来看看JDK的文档:
boolean java.util.regex.Pattern.matches(String regex, CharSequence input)
Compiles the given regular expression and attempts to match the given input against it.
An invocation of this convenience method of the form
Pattern.matches(regex, input);
behaves in exactly the same way as the expression
Pattern.compile(regex).matcher(input).matches()
If a pattern is to be used multiple times, compiling it once and reusing it will be more efficient than invoking this method each time.
Parameters:regex The expression to be compiledinput The character sequence to be matchedReturns:whether or not the regular expression matches on the inputThrows
:PatternSyntaxException -
If the expression's syntax is invalid
说明还可以这样使用:
boolean ty=Pattern.compile("^[a-zA-Z]\\w*$").matcher(tx).matches();
JAVA Pattern.matches的使用相关推荐
- java pattern matches,Java正则表达式中的Pattern.matches()方法
java.util.regex.Pattern.matches()方法匹配正则表达式和给定的输入.它有两个参数,即正则表达式和输入.如果正则表达式和输入匹配,则返回true,否则返回false. 给出 ...
- java正则表达式中的坑String.matches(regex)、Pattern.matches(regex, str)和Matcher.matches()
问题:程序会计算表达式的值 //将数值转换以K为单位 String value = "10*1000*1000"; String regex="\\s*\\*\\s*10 ...
- java pattern用法_Java Pattern和Matcher用法
Pattern用法 Java正则表达式通过java.util.regex包下的Pattern和Matcher类实现 Pattern类用于创建一个正则表达式,也可以说是创建一个匹配模式,可以通过两个静态 ...
- Java Pattern类的用法详解(正则表达式)
转载于:https://www.cnblogs.com/sparkbj/articles/6207103.html 正则表达式的编译表示形式. 指定为字符串的正则表达式必须首先被编译为此类的实例.然后 ...
- Java Pattern类和Matcher类的使用
java.util.regex 是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包.它包括两个类:Pattern 和 Matcher. Pattern 对象是正则表达式编译后在内存中的表示形 ...
- java 正则 pattern 线程安全_(一)Java Pattern类----java正则
记下一些知识..... Java Pattern类 Pattern在java.util.regex包中,是正则表达式的编译表示形式,此类的实例是不可变的,可供多个并发线程安全使用. 定义 public ...
- java pattern详解_JAVA正则表达式:Pattern类与Matcher类详解
以下我们就分别来看看这两个类: 一.捕获组的概念 捕获组可以通过从左到右计算其开括号来编号,编号是从1 开始的.例如,在表达式 ((A)(B(C)))中,存在四个这样的组: 1 ((A) ...
- java Pattern和Matcher详解
结论:Pattern与Matcher一起合作.Matcher类提供了对正则表达式的分组支持,以及对正则表达式的多次匹配支持. 单独用Pattern只能使用Pattern.matcher(String ...
- java 里面matches什么意思_java中的matches()方法
--来自百度知道里的回答 java.lang包中的String类,java.util.regex包中的Pattern,Matcher类中都有matches()方法. 都与正则表达式有关.举例:(字符串 ...
最新文章
- 实现O(1)时间复杂度带有min和max 函数的栈
- Effective Java之避免创建不必要的对象
- pandas将表中的字符串转成数值型
- 清理apache共享内存引起的oracle宕机
- 图像局部显著性—点特征(GLOH)
- matlab生成均匀部分散点图,应用halton序列生成均匀散点图
- xflash里的hello world程序
- pg数据库开启远程连接_Postgresql开启远程访问的步骤全纪录
- 聚焦 | 数据湖分析如何面向对象存储OSS进行优化?
- SpringCloud与zuul
- Unity3D研究院之2D游戏开发制作原理(二十一) 【转】
- 打开桌面计算机投屏到扩展屏,无线投屏新玩法——Windows电脑扩展屏幕投屏
- linux压缩文件zip,在 Linux 上压缩文件:zip 命令的各种变体及用法
- 计算机英语单词练习四
- XXL之整合SpringBoot
- 文案排版(参考中文文案排版指北)
- Youtube 评论数
- Unity 自发光(燃烧)溶解Shader
- LaTeX 各种写法
- ESimCSE:无监督句子表示对比学习的增强样本构建方法