Note that there are some explanatory texts on larger screens.

plurals
  1. PORegular Expressions to match an <a> tag
    text
    copied!<p>I am writing a small java program for a class, and I can't quite figure out why my regex isn't working properly. In the special case of having 2 tags on the same line that is read in, it only matches the second one.</p> <p>Here is a link that has the regex included, along with a simple set of test data: <a href="http://regexpal.com/?flags=&amp;regex=%5CB%3Ca.%2ahref=%28?%3a%27%7C%22%29%28http://%28?%3a%5BA-Za-z0-9%5C.%5D%2b%29%28?%3a%5BA-Za-z0-9/%5D%2b%5C.%28?%3ahtml%7Chtm%29%29?%29%28?%3a%27%7C%22%29%5B%5E%3E%5D%2a%3E%5Ba-zA-Z0-9%5D%2b%3C/a%3E%5CB&amp;input=%3Chtml%3E%0A%3Chead%3E%0A%3C/head%3E%0A%3Cbody%3E%0A%3Ca%20href=%22http://www.google.com%22%3EGoogle%3C/a%3E%0A%3Ca%20href=%22http://www.google.com/index.html%22%3EGoogle%3C/a%3E%3Ca%20href=%22http://www.google.com/index.htm%22%3EGoogle%3C/a%3E%0A%3Ca%20href=%22http://www.google.com/index.php%22%3EGoogle%3C/a%3E%0A%3Ca%20href=%22http://google.com%22%3EGoogle%3C/a%3E%0A%3Ca%20href=%22http://google.com/index.html%22%3EGoogle%3C/a%3E%0A%3Ca%20href=%22http://google.com/index.htm%22%3EGoogle%3C/a%3E%0A%3Ca%20href=%22http://google.com/index.php%22%3EGoogle%3C/a%3E%0A%3C/body%3E%0A%3C/html%3E" rel="nofollow">Regex Test Link</a>.</p> <p>In my java program I have the following code:</p> <pre><code>Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); String[] results; System.out.println(p.toString()); Matcher m = null; while((line = input.readLine()) != null) { m = p.matcher(line); while(m.find()) { System.out.println("Matches: " + m.group(1)); } } </code></pre> <p>The goal is to extract the href value, as long as it starts with http://, the website ends in either no page (like <a href="http://www.google.com" rel="nofollow">http://www.google.com</a>) or ends in index.htm or index.html (like <a href="http://www.google.com/index.html" rel="nofollow">http://www.google.com/index.html</a>).</p> <p>My regex works for every case of the above, but doesnt match in the special case of 2 tags that are on the same line.</p> <p>Any help is appreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload