문제

I am dying trying to figure out why a regex won't match. Any help is much appreciated. I'm going line by line of a web page (that works fine), but I need to pull out the links for each line. The application will check to see if there is a link in the line, but I need to actually pull out the URL. help?

Pattern p = Pattern.compile("^.*href=\"([^\"]*)");
Matcher m = p.matcher(result);
String urlStr = m.group();
links.add(urlStr);

The error message I keep getting is this:

Exception in thread "main" java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.group(Matcher.java:485)

Even though 'result' has a link reference (hxxp://www.yahoo.com) in it.

links is an ArrayList fyi. Thanks in advance!

도움이 되었습니까?

해결책 2

Ok, so I figured it out. The only issue was, my regex had to be

".*href=\"([^\"]*?)\".*"

Which then made the code

private String regex = ".*href=\"([^\"]*?)\".*";
private Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(result);
if (m.matches()) {
    String urlStr = m.group(1);
    links.add(urlStr);
}

So that was my issue with the regex, I had to use the '?' to not be greedy I guess!

다른 팁

first call

m.find();

or

m.matches();

and then you'll be able to use m.group() if matcher succeeded.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top