title: Java regex to capture html tag and its attributes
body: <p>(I am aware that regex is not the recommended way to deal with html, but this is my assignment)</p>
  
  <p>I need a regex in Java that will capture html tags and their attributes. I am trying to achieve this with one regex using groups. I expected this regex to work:</p>
  
  <pre><code>&lt;(?!!)(?!/)\s*(\w+)(?:\s*(\S+)=['"]{1}[^&gt;]*?['"]{1})*\s*&gt;
  &lt;                                                            the tag starts with &lt;
   (?!!)                                                       I dont't want comments
        (?!/)                                                  I dont't want closing tags
             \s*                                               any number of white spaces 
                (\w+)                                          the tag
                     (?:                                       do not capture the following group
                        \s*                                    any number of white spaces before the first attribute
                           (\S+)                               capture the attributes name
                                =['"]{1}[^&gt;]*?['"]{1}          the ="bottm" or ='bottm' etc.
                                                     )*        close the not-capturing group, it can occure multiple times or zero times
                                                       \s*     any white spaces before the closing of the tag
                                                          &gt;    close the tag
  </code></pre>
  
  <p>I expected the result for a tag like:</p>
  
  <pre><code>&lt;div id="qwerty" class='someClass' &gt;
  group(1) = "div"
  group(2) = "id"
  group(3) = "class"   
  </code></pre>
  
  <p>but the result is:</p>
  
  <pre><code>group(1) = "div"
  group(2) = "class"
  </code></pre>
  
  <p>Is seems that it is not possible to capture a group multiple times (...)*, is this correct?</p>
  
  <p>As for now I use a reg ex like:     </p>
  
  <pre><code>&lt;(?!!)(?!/)\s*(\w+) (?:\s*(\S+)=['"]{1}[^&gt;]*?['"]{1}){0,1} (?:\s*(\S+)=['"]{1}[^&gt;]*?['"]{1}){0,1} (...){0,1} (...){0,1} ... \s*&gt;
  </code></pre>
  
  <p>I repeat the capturing group for the attribute multiple times and get results like:</p>
  
  <pre><code>&lt;div id="qwerty" class='someClass' &gt;
  group(1) = "div"
  group(2) = "id"
  group(3) = "class" 
  group(4) = null 
  group(5) = null 
  group(6) = null 
  ...
  </code></pre>
  
  <p>What other approaches can I use? (I can use multiple regexes, but it is preferred to do it with just one)</p>