Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Next time you need a regex explained, you can use the following <a href="http://rick.measham.id.au/paste/explain.pl" rel="noreferrer"><code>explain.pl</code></a> service from Rick Measham:</p> <pre><code>Regex: [^,]*|.+(,).+ NODE EXPLANATION -------------------------------------------------------------------------------- [^,]* any character except: ',' (0 or more times (matching the most amount possible)) -------------------------------------------------------------------------------- | OR -------------------------------------------------------------------------------- .+ any character except \n (1 or more times (matching the most amount possible)) -------------------------------------------------------------------------------- ( group and capture to \1: -------------------------------------------------------------------------------- , ',' -------------------------------------------------------------------------------- ) end of \1 -------------------------------------------------------------------------------- .+ any character except \n (1 or more times (matching the most amount possible)) </code></pre> <hr> <pre><code>Regex: (\()?\d+(?(1)\)) NODE EXPLANATION -------------------------------------------------------------------------------- ( group and capture to \1 (optional (matching the most amount possible)): -------------------------------------------------------------------------------- \( '(' -------------------------------------------------------------------------------- )? end of \1 (NOTE: because you're using a quantifier on this capture, only the LAST repetition of the captured pattern will be stored in \1) -------------------------------------------------------------------------------- \d+ digits (0-9) (1 or more times (matching the most amount possible)) -------------------------------------------------------------------------------- (?(1) if back-reference \1 matched, then: -------------------------------------------------------------------------------- \) ')' -------------------------------------------------------------------------------- | else: -------------------------------------------------------------------------------- succeed -------------------------------------------------------------------------------- ) end of conditional on \1 </code></pre> <h3>Links</h3> <ul> <li><a href="http://rick.measham.id.au/paste/explain.pl" rel="noreferrer">http://rick.measham.id.au/paste/explain.pl</a></li> </ul> <hr> <h3>Note on conditionals</h3> <p>JAVA DOES NOT SUPPORT CONDITIONALS! An unconditionalized regex for the second pattern would be something like:</p> <pre><code>\d+|\(\d+\) </code></pre> <p>i.e. a non-zero repetition of digits, with or without surrounding parentheses.</p> <h3>Links</h3> <ul> <li><a href="http://www.regular-expressions.info/conditional.html" rel="noreferrer">regular-expressions.info/If-then-else conditionals</a> <blockquote> <p>Conditionals are supported by the JGsoft engine, Perl, PCRE and the .NET framework.</p> </blockquote></li> </ul> <hr> <h3>The patterns in depth</h3> <p>Here's a test harness for the first pattern</p> <pre><code> import java.util.regex.*; //... Pattern p = Pattern.compile("[^,]*|.+(,).+"); String[] tests = { "", // [] is a match with no commas "abc", // [abc] is a match with no commas ",abc", // [,abc] is not a match "abc,", // [abc,] is not a match "ab,c", // [ab,c] is a match with separating comma "ab,c,", // [ab,c,] is a match with separating comma ",", // [,] is not a match ",,", // [,,] is not a match ",,,", // [,,,] is a match with separating comma }; for (String test : tests) { Matcher m = p.matcher(test); System.out.format("[%s] is %s %n", test, !m.matches() ? "not a match" : m.group(1) != null ? "a match with separating comma" : "a match with no commas" ); } </code></pre> <h3>Conclusion</h3> <ul> <li>To match, the string must fall into one of these two cases: <ul> <li>Contains no comma (potentially an empty string)</li> <li>Contains a comma that separates two non-empty strings</li> </ul></li> <li>On a match, <code>\1</code> can be used to distinguish between the two cases</li> </ul> <p>And here's a similar test harness for the second pattern, rewritten without using conditionals (which isn't supported by Java):</p> <pre><code> Pattern p = Pattern.compile("\\d+|(\\()\\d+\\)"); String[] tests = { "", // [] is not a match "0", // [0] is a match without parenthesis "(0)", // [(0)] is a match with surrounding parenthesis "007", // [007] is a match without parenthesis "(007)", // [(007)] is a match with surrounding parenthesis "(007", // [(007] is not a match "007)", // [007)] is not a match "-1", // [-1] is not a match }; for (String test : tests) { Matcher m = p.matcher(test); System.out.format("[%s] is %s %n", test, !m.matches() ? "not a match" : m.group(1) != null ? "a match with surrounding parenthesis" : "a match without parenthesis" ); } </code></pre> <p>As previously said, this matches a non-zero number of digits, possibly surrounded by parenthesis (and <code>\1</code> distinguishes between the two).</p>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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