V
V
Viktor Korzunin2013-07-12 19:41:05
Java
Viktor Korzunin, 2013-07-12 19:41:05

Question about Matches in regex (Java)?

There is this code:

public class Test {
  public static void main(String[] args) {
    String htmlCode = getHtmlCode("http://www.некий_сайт.ru");
    Pattern pattern = Pattern.compile("<a href=.*?>(.*?)</a>");
    Matcher matcher = pattern.matcher(htmlCode);
    if (matcher.matches()) {
      System.out.println(1);
    }
    System.exit(0);
  }
  
  public static String getHtmlCode (String strURL) {
    String str = "";
    try {
      URL url = new URL(strURL);
      BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
          String line;
          while ((line = reader.readLine()) != null) {str += line;}
          reader.close();
    } catch (MalformedURLException e) {e.printStackTrace();
    } catch (UnknownHostException e) {e.printStackTrace();
    } catch (IOException e) {e.printStackTrace();}
    return str;
  }
}

Why does matcher.matches() always return False?
I suspect some inconsistencies with the CharSequence type, but I can't figure it out
Help?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Victor Korzunin, 2013-07-12
@F1oyd

Ahhh, he asked everything himself answered:
instead of matcher.matches() you need matcher.find()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question