Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's wrong with my regex pattern?
    primarykey
    data
    text
    <p>I'm asked to catch any html tag using regular expression: </p> <pre><code>A. &lt;TAG ATTRIBUTE="VALUE"/&gt; or B. &lt;TAG ATTRIBUTE="VALUE"&gt; or C. &lt;TAG/&gt; or D. &lt;TAG&gt; or E. &lt;/TAG&gt; </code></pre> <p>Here is my pattern:</p> <pre><code>/** A pattern that matches a simple HTML markup. Group 1 matches * the initial '/', if present. Group 2 matches the tag. Group * 3 matches the attribute name, if present. Group 4 matches the * attribute value (without quotes). Group 5 matches the closing * '/', if present. */ public static final String HTML_P3 = "&lt;(/)?\\s*([a-zA-Z]+)\\s*([a-zA-Z]+)?\\s*=?\\s*\\\"?([^\\\"]+)?\\\"?\\s*(/)?&gt;"; </code></pre> <p>Here is a snippet of the test given:</p> <pre><code>public static void p3(String name, String markup) throws IOException { out.println("Problem #3."); Scanner inp = new Scanner(new FileReader(name)); while (inp.findWithinHorizon(markup, 0) != null) { MatchResult mat = inp.match(); if (mat.group(1) != null &amp;&amp; (mat.group(5) != null || mat.group(3) != null)) { out.printf("Bad markup.%n"); continue; } out.printf("Tag: %s", mat.group(2)); if (mat.group(3) != null) { out.printf(", Attribute: %s, Value: \"%s\"", mat.group(3), mat.group(4)); } if (mat.group(5) != null || mat.group(1) != null) { out.print(" end"); } out.println(); } out.println(); } </code></pre> <p>Here is the input:</p> <pre><code>This is a simple &lt;i&gt;mark-up&lt;/i&gt;. Next comes one &lt;input value="3"/&gt; that's closed, followed by a list of names: &lt;ol color="green"&gt; &lt;li&gt; Tom &lt;/li&gt; &lt;li &gt; Dick &lt;/li&gt; &lt;li&gt; Harry &lt;/li&gt; &lt;/ol&gt; </code></pre> <p>The correct answer should be:</p> <pre><code>Problem #3. Tag: i Tag: i end Tag: input, Attribute: value, Value: "3" end Tag: ol, Attribute: color, Value: "green" Tag: li Tag: li end Tag: li Tag: li end Tag: li Tag: li end Tag: ol end </code></pre> <p>However, I can never catch any ending tag, and here is my output:</p> <pre><code>Problem #3. Tag: i Tag: input, Attribute: value, Value: "3" end Tag: ol, Attribute: color, Value: "green" Tag: li </code></pre> <p>I've tried using regexpal.com and my pattern matches everything. Can someone shed some lights please?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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