<p>Using regular expressions, what is the simplest way to fetch a websites HTML and find the value inside this tag (or any attribute's value for that matter):</p>

<pre><code>&lt;html&gt;
  &lt;head&gt;
  [snip]
  &lt;meta name="generator" value="thevalue i'm looking for" /&gt;
  [snip]
</code></pre>

[{'answer_id': 31432, 'author': 'Mike Haboustak', 'author_id': 2146, 'author_profile': 'https://Stackoverflow.com/users/2146', 'pm_score': 4, 'selected': True, 'text': '<p>Depends on how sophisticated of an Http request you need to build (authentication, etc). Here\'s one simple way I\'ve seen used in the past. </p>\n\n<pre><code>StringBuilder html = new StringBuilder();\njava.net.URL url = new URL("http://www.google.com/");\nBufferedReader input = null;\ntry {\n    input new BufferedReader(\n        new InputStreamReader(url.openStream()));\n\n    String htmlLine;\n    while ((htmlLine=input.readLine())!=null) {\n        html.appendLine(htmlLine);\n    }\n}\nfinally {\n    input.close();\n}\n\nPattern exp = Pattern.compile(\n    "&lt;meta name=\\"generator\\" value=\\"([^\\"]*)\\" /&gt;");\nMatcher matcher = exp.matcher(html.toString());\nif(matcher.find())\n{\n    System.out.println("Generator: "+matcher.group(1));\n}\n</code></pre>\n\n<p><em>Probably plenty of typos here to be found when compiled.\n(hope this wasn\'t homework)</em></p>\n'}, {'answer_id': 31464, 'author': 'Justin Bennett', 'author_id': 271, 'author_profile': 'https://Stackoverflow.com/users/271', 'pm_score': 0, 'selected': False, 'text': '<p>You may want to check the documentation for Apache\'s org.apache.commons.HttpClient package and the related packages <a href="http://hc.apache.org/httpclient-3.x/apidocs/index.html" rel="nofollow noreferrer">here</a>.  Sending an HTTP request from a Java application is pretty easy to do.  Poking through the documentation should get you off in the right direction.</p>\n'}, {'answer_id': 31467, 'author': 'Paul Tomblin', 'author_id': 3333, 'author_profile': 'https://Stackoverflow.com/users/3333', 'pm_score': 0, 'selected': False, 'text': "<p>I haven't tried this, but wouldn't the basic framework be</p>\n\n<ol>\n<li>Open a <code>java.net.HttpURLConnection</code></li>\n<li>Get an input stream using <code>getInputStream</code></li>\n<li>Use the regular expression in Mike's answer to parse out the bit you want</li>\n</ol>\n"}, {'answer_id': 101117, 'author': 'Eek', 'author_id': 18752, 'author_profile': 'https://Stackoverflow.com/users/18752', 'pm_score': 0, 'selected': False, 'text': '<p>Strictly speaking you can\'t really be sure you got the right value, since the meta tag may be commented out, or the meta tag may be in uppercase etc. It depends on how certain you are that the HTML can be considered as "nice".</p>\n'}, {'answer_id': 137216, 'author': 'vrdhn', 'author_id': 414441, 'author_profile': 'https://Stackoverflow.com/users/414441', 'pm_score': 1, 'selected': False, 'text': '<p>You should be using XPath query.</p>\n\n<p>It\'s as simple as getting value of <code>/html/head/meta[@name=generator]/@value</code>.</p>\n\n<p>A good tutorial: <a href="http://www.onjava.com/pub/a/onjava/2005/01/12/xpath.html" rel="nofollow noreferrer">Parsing an XML Document with XPath</a></p>\n'}, {'answer_id': 1778256, 'author': 'Stephen C', 'author_id': 139985, 'author_profile': 'https://Stackoverflow.com/users/139985', 'pm_score': 0, 'selected': False, 'text': '<p>It depends.</p>\n\n<p>If you are extracting information from a site or sites that are guaranteed to be well-formed HTML, and you know that the &lt;meta&gt; won\'t be obfuscated in some way then a reading the &lt;head&gt; section line by line and applying a regex is a good approach.</p>\n\n<p>On the other hand, if the HTML may be mangled or "tricky" then you need to use a proper HTML parser, possibly a permissive one like HTMLTidy.  Beware of using a strict HTML or XML parser on stuff trawled from random websites.  Lots of so-called HTML you find out there is actually malformed.</p>\n'}, {'answer_id': 4465801, 'author': 'Mads Burgandy', 'author_id': 545384, 'author_profile': 'https://Stackoverflow.com/users/545384', 'pm_score': 2, 'selected': False, 'text': '<p>Its amazing how noone, when addressing the problem of using RegEx with HTML, confronts the problem of HTML often <strong>NOT</strong> being well-formed, thus rendering a lot of HTML-parsers completely useless.</p>\n\n<p>If you are developing tools to analyze webpages and its a fact that these are not well-formed HTML, the statement "Regex should never be used to parse HTML" og "use a HTML parser" is just completely bogus. Facts are that in the real world, people create HTML as they feel like - and not necessarily suited for parsers.</p>\n\n<p>RegEx <em>is</em> a completely valid way to find elements in text, thus in HTML. If there are any other reasonable way to confront the problems the Original Poster has, then post them instead of referring to a "use a parser" or "RTFM" statement.</p>\n'}]
['https://Stackoverflow.com/questions/31415', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2644/']