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的使用相关推荐

  1. java pattern matches,Java正则表达式中的Pattern.matches()方法

    java.util.regex.Pattern.matches()方法匹配正则表达式和给定的输入.它有两个参数,即正则表达式和输入.如果正则表达式和输入匹配,则返回true,否则返回false. 给出 ...

  2. java正则表达式中的坑String.matches(regex)、Pattern.matches(regex, str)和Matcher.matches()

    问题:程序会计算表达式的值 //将数值转换以K为单位 String value = "10*1000*1000"; String regex="\\s*\\*\\s*10 ...

  3. java pattern用法_Java Pattern和Matcher用法

    Pattern用法 Java正则表达式通过java.util.regex包下的Pattern和Matcher类实现 Pattern类用于创建一个正则表达式,也可以说是创建一个匹配模式,可以通过两个静态 ...

  4. Java Pattern类的用法详解(正则表达式)

    转载于:https://www.cnblogs.com/sparkbj/articles/6207103.html 正则表达式的编译表示形式. 指定为字符串的正则表达式必须首先被编译为此类的实例.然后 ...

  5. Java Pattern类和Matcher类的使用

    java.util.regex 是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包.它包括两个类:Pattern 和 Matcher. Pattern 对象是正则表达式编译后在内存中的表示形 ...

  6. java 正则 pattern 线程安全_(一)Java Pattern类----java正则

    记下一些知识..... Java Pattern类 Pattern在java.util.regex包中,是正则表达式的编译表示形式,此类的实例是不可变的,可供多个并发线程安全使用. 定义 public ...

  7. java pattern详解_JAVA正则表达式:Pattern类与Matcher类详解

    以下我们就分别来看看这两个类: 一.捕获组的概念 捕获组可以通过从左到右计算其开括号来编号,编号是从1 开始的.例如,在表达式 ((A)(B(C)))中,存在四个这样的组: 1        ((A) ...

  8. java Pattern和Matcher详解

    结论:Pattern与Matcher一起合作.Matcher类提供了对正则表达式的分组支持,以及对正则表达式的多次匹配支持. 单独用Pattern只能使用Pattern.matcher(String ...

  9. java 里面matches什么意思_java中的matches()方法

    --来自百度知道里的回答 java.lang包中的String类,java.util.regex包中的Pattern,Matcher类中都有matches()方法. 都与正则表达式有关.举例:(字符串 ...

最新文章

  1. 实现O(1)时间复杂度带有min和max 函数的栈
  2. Effective Java之避免创建不必要的对象
  3. pandas将表中的字符串转成数值型
  4. 清理apache共享内存引起的oracle宕机
  5. 图像局部显著性—点特征(GLOH)
  6. matlab生成均匀部分散点图,应用halton序列生成均匀散点图
  7. xflash里的hello world程序
  8. pg数据库开启远程连接_Postgresql开启远程访问的步骤全纪录
  9. 聚焦 | 数据湖分析如何面向对象存储OSS进行优化?
  10. SpringCloud与zuul
  11. Unity3D研究院之2D游戏开发制作原理(二十一) 【转】
  12. 打开桌面计算机投屏到扩展屏,无线投屏新玩法——Windows电脑扩展屏幕投屏
  13. linux压缩文件zip,在 Linux 上压缩文件:zip 命令的各种变体及用法
  14. 计算机英语单词练习四
  15. XXL之整合SpringBoot
  16. 文案排版(参考中文文案排版指北)
  17. Youtube 评论数
  18. Unity 自发光(燃烧)溶解Shader
  19. LaTeX 各种写法
  20. ESimCSE:无监督句子表示对比学习的增强样本构建方法

热门文章

  1. error C2057: 应输入常量表达式
  2. 【How to Read a Paper Efficiently】如何高效阅读论文
  3. Elasticsearch常见unassigned原因和解决方式
  4. elasticsearch unassigned错误解决
  5. codefile codebehind
  6. 节日流水灯c语言编程,流水灯C语言编程
  7. ServerU FTP服务器无法上传中文名文件怎么办
  8. 目前流行的几种排课算法的介绍
  9. 常见的8种大数据分析模型,你了解吗
  10. android加载百分比功能,android进度条怎么显示百分比