Java String matches示例
时间:2020-02-23 14:35:14 来源:igfitidea点击:
在本教程中,我们将看到String的匹配方法。
字符串的匹配方法
字符串匹配方法用于检查字符串是否与给定的正则表达式匹配。
在正常术语中,我们可以通过各种正则表达式来检查指定的字符串是否与该正则表达式匹配。
如果匹配成功返回true否则为false。
字符串匹配方法如果给定的正则表达式无效,则抛出PatternsyntaxException。
语法:
public boolean matches(String regex) //regex is regular expression.
如何使用matches方法:
字符串匹配方法使用正则表达式。
所以,两个广泛使用的正则表达式元字符是点(.)和星号(*)。
点号(.)与任何字符匹配。
星号匹配任意次数。
根据我们想要匹配的模式,构建正则表达式。
下面给出的示例执行以下任务:
检查字符串是否包含任何数字数字或者不是:[0-9]
检查字符串是否包含任何字符或者不是EX:[A-ZA-Z]。
检查字符串包含给定的单词或者不是前:(John)
对于所有这些,我们可以创建正则表达式并使用String的Matches方法检查它。
字符串匹配示例:
package org.igi.theitroad;
public class JavaStringMatchesMain
{
public static void main(String args[])
{
String str="Hello i am john,id is 1 and my seat number is 88";
System.out.println(str);
//showing numeric pattern matching
System.out.println("===================");
System.out.println("Number matching example");
System.out.println("===================");
boolean b=str.matches(".*[1-9].*");
System.out.println("pattern: .*[1-9].* matches => "+b);
b=str.matches(".*(100).*");
System.out.println("pattern: .*(100).* matches => "+b);
b=str.matches(".*(88).*");
System.out.println("pattern: .*(88).* matches => "+b);
//showing character matching
System.out.println("===================");
System.out.println("Character matching example");
System.out.println("===================");
b=str.matches(".*[A-Za-z].*");
System.out.println("pattern: .*[A-Za-z].* matches => "+b);
b=str.matches(".*[A].*");
System.out.println("pattern: .*[A].* matches => "+b);
b=str.matches(".*[i].*");
System.out.println("pattern: .*[i].* matches => "+b);
System.out.println("===================");
System.out.println("Word and String matching example");
System.out.println("===================");
//showing word and string matching
b=str.matches(".*(john).*");
System.out.println("pattern: .*(john).* matches => "+b);
b=str.matches(".*(Hello i am john).*");
System.out.println("pattern: .*(Hello i am john).* matches => "+b);
b=str.matches(".*(JOHN).*");
System.out.println("pattern: .*(JOHN).*matches => "+b);
System.out.println();
String str1="Hello i am John Adam";
System.out.println(str1);
b=str1.matches(".*(Adam).*"); //ignore after and before words
System.out.println("pattern: .*(Adam).* matches => "+b);
b=str.matches("John"); //just match for single word john
System.out.println("pattern: John matches => "+b);
}
}
输出:
Hello i am john,id is 1 and my seat number is 88 =================== Number matching example =================== pattern: .*[1-9].* matches => true pattern: .*(100).* matches => false pattern: .*(88).* matches => true =================== Character matching example =================== pattern: .*[A-Za-z].* matches => true pattern: .*[A].* matches => false pattern: .*[i].* matches => true =================== Word and String matching example =================== pattern: .*(john).* matches => true pattern: .*(Hello i am john).* matches => true pattern: .*(JOHN).*matches => falseHello i am johnMehra pattern: .*(Mehra).* matches => true pattern: john matches => false

