Java 字符串不匹配,即使是;“模式字符串”;是“的子字符串”;“文本”;
我试图学习Java正则表达式,并试图将一个较小的字符串与另一个字符串进行匹配。下面是我想出的代码Java 字符串不匹配,即使是;“模式字符串”;是“的子字符串”;“文本”;,java,regex,Java,Regex,我试图学习Java正则表达式,并试图将一个较小的字符串与另一个字符串进行匹配。下面是我想出的代码 String text = "this is the text to be searched for occurrences of the http://www.nba.com."; String patternString = "http://.*"; Pattern p1 = Pattern.compile(patternString); Matcher m1 = p1.
String text = "this is the text to be searched for occurrences of the http://www.nba.com.";
String patternString = "http://.*";
Pattern p1 = Pattern.compile(patternString);
Matcher m1 = p1.matcher(text);
boolean doesItMatch = m1.matches();
System.out.println(doesItMatch);
System.out.println(m1.group());
我希望doesItMatch
等于true
和m1.group()
等于http://nba.com.
。但是,IDE将改为输出
false
Exception in thread "main" java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.group(Matcher.java:536)
at java.util.regex.Matcher.group(Matcher.java:496)
at JTORegex.RegularExpression.main(RegularExpression.java:23)
Java Result: 1
为什么字符串模式字符串
与字符串文本
不匹配<代码>模式字符串确实存在于文本
中。为什么会这样?怎么样
boolean doesItMatch = m1.matches();
if (doesItMatch)
System.out.println(m1.group());
匹配
匹配完整字符串。对部分匹配使用find
boolean hasAMatch = m1.find();
你可以用
varname.find();
或者用布尔变量实例化它
boolean newvar = varname.find();