Java matches() 方法

Java matches() 方法

Java String类


matches() 方法用于检测字符串是否匹配给定的正则表达式。

调用此方法的 str.matches(regex) 形式与以下表达式产生的结果完全相同:

Pattern.matches(regex, str)

语法

public boolean matches(String regex)

参数

  • regex — 匹配字符串的正则表达式。

返回值

在字符串匹配给定的正则表达式时,返回 true。

实例

public class Test {
	public static void main(String args[]) {
		String Str = new String("www.w3cschool.cn");

		System.out.print("返回值 :" );
		System.out.println(Str.matches("(.*)youj(.*)"));

		System.out.print("返回值 :" );
		System.out.println(Str.matches("(.*)google(.*)"));

		System.out.print("返回值 :" );
		System.out.println(Str.matches("www(.*)"));
	}
}

以上程序执行结果为:

返回值 :false
返回值 :false
返回值 :true

Java String类

版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《Java matches() 方法》
文章链接:https://zhuji.vsping.com/295284.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。