title: java Regular Expression to extract page title
body: <p>I am trying to extract page title from HTML and XML pages.  This is the regular expression I use:</p>
  
  <pre><code>Pattern p = Pattern.compile(".*&lt;head&gt;.*&lt;title&gt;(.*)&lt;/title&gt;.*&lt;/head&gt;.*");
  </code></pre>
  
  <p>The problem is that it only extracts the title from HTML files and gives me null for XML files.  Can any one help me in changing the regex to the get the XML page titles as well?</p>
  
  <p>Code:</p>
  
  <pre><code>content= stringBuilder.toString(); // put content of the file as a string
  Pattern p = Pattern.compile(".*&lt;head&gt;.*&lt;title&gt;(.*)&lt;/title&gt;.*&lt;/head&gt;.*");
  Matcher m = p.matcher(content);
  while (m.find()) {
      title = m.group(1);
  }
  </code></pre>