Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Condition in Regular Expressions
    text
    copied!<p>eg:<br> <b>Text:</b></p> <p><code>&lt;!-- Nav bar --&gt;</code></p> <p><code>&lt;TD&gt;</code></p> <p><code>&lt;A HREF="/home"&gt;&lt;IMG SRC="/images/home.gif"&gt;&lt;/A&gt;</code></p> <p><code>&lt;IMG SRC="/images/spacer.gif"&gt;</code></p> <p><code>&lt;A HREF="/search"&gt;&lt;IMG SRC="/images/search.gif"&gt;&lt;/A&gt;</code></p> <p><code>&lt;IMG SRC="/images/spacer.gif"&gt;</code></p> <p><code>&lt;A HREF="/help"&gt;&lt;IMG SRC="/images/help.gif"&gt;&lt;/A&gt;</code></p> <p><code>&lt;/TD&gt;</code></p> <p><b>Regex:</b></p> <pre><code> (&lt;[Aa]\s+[^&gt;]+&gt;\s*)?&lt;[Ii][Mm][Gg]\s+[^&gt;]+&gt;(?(1)\s*&lt;/[Aa]&gt;) </code></pre> <p><b>Result:</b></p> <p><code>&lt;A HREF="/home"&gt;&lt;IMG SRC="/images/home.gif"&gt;&lt;/A&gt;</code></p> <p><code>&lt;IMG SRC="/images/spacer.gif"&gt;</code></p> <p><code>&lt;A HREF="/search"&gt;&lt;IMG SRC="/images/search.gif"&gt;&lt;/A&gt;</code></p> <p><code>&lt;IMG SRC="/images/spacer.gif"&gt;</code></p> <p><code>&lt;A HREF="/help"&gt;&lt;IMG SRC="/images/help.gif"&gt;&lt;/A&gt;</code><br><br><br></p> <p>what's the "?(1)" mean?<br/></p> <p>When I run it in Java ,it cause a exception: java.util.regex.PatternSyntaxException,the "?(1)" can't be recognized.</p> <p>The explanation in the book is :</p> <p><code>This pattern requires explanation. (&lt;[Aa]\s+[^&gt;]+&gt;\s*)? matches an opening &lt;A&gt; or &lt;a&gt; tag (with any attributes that may be present), if present (the closing ? makes the expression optional). &lt;[Ii][Mm][Gg]\s+[^&gt;]+&gt; then matches the &lt;IMG&gt; tag (regardless of case) with any of its attributes. (?(1)\s*&lt;/[Aa]&gt;) starts off with a condition: ?(1) means execute only what comes next if backreference 1 (the opening &lt;A&gt; tag) exists (or in other words, execute only what comes next if the first &lt;A&gt; match was successful). If (1) exists, then \s*&lt;/[Aa]&gt; matches any trailing whitespace followed by the closing &lt;/A&gt; tag.</code></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