Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your non-trivial regex: <code>^[a-zA-Z]([-\s\.a-zA-Z]*('(?!'))?[-\s\.a-zA-Z]*)*$</code>, is better written with comments in free-spacing mode like so:</p> <pre class="lang-cs prettyprint-override"><code>Regex re_orig = new Regex(@" ^ # Anchor to start of string. [a-zA-Z] # First char must be letter. ( # $1: Zero or more additional parts. [-\s\.a-zA-Z]* # Zero or more valid name chars. ( # $2: optional quote. ' # Allow quote but only (?!') # if not followed by quote. )? # End $2: optional quote. [-\s\.a-zA-Z]* # Zero or more valid name chars. )* # End $1: Zero or more additional parts. $ # Anchor to end of string. ",RegexOptions.IgnorePatternWhitespace); </code></pre> <p>In English, this regex essentially says: <em>"Match a string that begins with an alpha letter <code>[a-zA-Z]</code> followed by zero or more alpha letters, whitespaces, periods, hyphens or single quotes, but each single quote may not be immediately followed by another single quote."</em></p> <p>Note that your above regex allows oddball names such as: <code>"ABC---...'... -.-.XYZ "</code> which may or may not be what you need. It also allows multi-line input and strings that end with whitespace.</p> <p>The "infinite loop" problem with the above regex is that <a href="http://www.regular-expressions.info/catastrophic.html" rel="nofollow">catastrophic backtracking</a> occurs when this regex is applied to a long invalid input which contains two single quotes in a row. Here is an equivalent pattern which matches (and fails to match) the exact same strings, but does not experience catastrophic backtracking:</p> <pre class="lang-cs prettyprint-override"><code>Regex re_fixed = new Regex(@" ^ # Anchor to start of string. [a-zA-Z] # First char must be letter. [-\s.a-zA-Z]* # Zero or more valid name chars. (?: # Zero or more isolated single quotes. ' # Allow single quote but only (?!') # if not followed by single quote. [-\s.a-zA-Z]* # Zero or more valid name chars. )* # Zero or more isolated single quotes. $ # Anchor to end of string. ",RegexOptions.IgnorePatternWhitespace); </code></pre> <p>And here it is in short form in your code context:</p> <pre class="lang-cs prettyprint-override"><code>const string PATTERN = @"^[a-zA-Z][-\s.a-zA-Z]*(?:'(?!')[-\s.a-zA-Z]*)*$"; </code></pre>
 

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