Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 字符串不匹配,即使是;“模式字符串”;是“的子字符串”;“文本”;_Java_Regex - Fatal编程技术网

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.

我试图学习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.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();