Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The answer is: no. You cannot use backreferences in character classes of .NET regular expressions. Sorry. See below for a workaround for your situation.</p> <blockquote> <p><em>"it shows that \p{name} is valid syntax in a character class"</em></p> </blockquote> <p>yes, it is. But the .NET documentation does not say that name will be interpreted from a backreference. It must be a unicode literal class string.</p> <blockquote> <p><em>"In other words, it doesn't take into account that each letter is distinct; no repeats."</em></p> </blockquote> <p>I understand that this means to match all of <code>e f a x</code> and only <code>f</code> and <code>x</code> in <code>e f e x</code>. In other words: match unique characters, do not match characters that are repeated.</p> <h2>Solution</h2> <p>I understand your question as follows: match all unique words (subexpressions, characters) in a string that have no repetition before or after itself. The basic regular expression you should use is this:</p> <pre><code>(subexpr)(?!.*\1)(?&lt;!\1.+) </code></pre> <p>which will find the word <code>subexpr</code> only when it appears once in the matching string. for instance, if we change it to match the <code>e</code> in <code>e f a x</code> and not in <code>e f e x</code>, it will look like this:</p> <pre><code>(e)(?!.*\1)(?&lt;!\1.+) </code></pre> <p>You can generalize this to match every unique letter in the string:</p> <pre><code>(.)(?!.*\1)(?&lt;!\1.+) </code></pre> <p>if will match <code>e</code>, <code>f</code>, <code>a</code> and <code>x</code> in <code>e f a x</code> and only <code>f</code> and <code>x</code> in <code>e f e x</code>. This could be the generalized replacement for your expression above and you don't need to repeat the 1,2,3 etc captures anymore.</p> <h2>How it works</h2> <p>(update) Perhaps nice to know how the above regex works:</p> <pre><code>(subexpr) # grab subexpression (can be any valid grouped regex) (?!.*\1) # negative look forward with a backrefence: if followed somewhere by itself, fail (?&lt;!\1.+) # negative look backward with backref: if preceded somewhere by itself, fail </code></pre> <h2>Applied solution</h2> <p>A word has a pattern. SUCCUBUS is 1 2 3 3 2 4 2 1. PAST is 1 2 3 4. Based on that pattern, a regex should match words with the same pattern: same length of the word, repetition of the same letters in the same place: PAST and RANT have the same pattern. LOOK and HEEL have the same pattern, but not HERE.</p> <p>Taking the previous solution, we adjust that generally to your problem domain by sticking to the following rules:</p> <ol> <li>A unique letter is represented by <code>(.)(?!.*\X)(?&lt;!\X.+)</code></li> <li>A repeated letter is represented by <code>(.)</code></li> <li>The location where repetition occurs is represented by <code>\X</code> (no parentheses!)</li> <li><code>\X</code> means a backreference with the number of your pattern</li> </ol> <p>Examples:</p> <pre><code># SUCCUBUS is 1 2 3 3 2 4 2 1 (only 4 is unique) (.) # nr 1 in pattern (.) # nr 2 in pattern (.) # nr 3 in pattern \3 # repeat 3 \2 # repeat 2 (.)(?!.*\4)(?&lt;!\4.+) # nr 4 UNIQUE! \2 # repeat 2 \1 # repeat 1 # PAST (all unique: 1 2 3 4) (.)(?!.*\1)(?&lt;!\1.+) # nr 1 in pattern (.)(?!.*\2)(?&lt;!\2.+) # nr 2 in pattern (.)(?!.*\3)(?&lt;!\3.+) # nr 3 in pattern (.)(?!.*\4)(?&lt;!\4.+) # nr 4 in pattern </code></pre> <p>This pattern should be easily automated into your current system.</p> <p>An excellent way to test this and other regexes (just copy and paste mine) is at <a href="http://regexhero.net/tester/" rel="nofollow noreferrer">Regex Hero, free online SilverLight .NET regex tester</a>. For other online testers, <a href="http://www.undermyhat.org/blog/2009/09/overview-of-online-regular-expression-testers/" rel="nofollow noreferrer">see my overview chart of them</a>.</p> <p><em>update: removed earlier irrelevant update note</em></p> <p><strong>Update 1:</strong> In a comment in the other solution, you say that you want to be able to match a substring that fits the pattern. Naturally, this poses a challenge with the negative look-forward/behind: as they are now, they look through the whole string. Replace the <code>.*</code> and <code>.+</code> with the relative length the expression would be at the place, pos 3 for PAST becomes then <code>(.)(?!.{1}\3)(?&lt;!\3.{2})</code> and pos 4 would become <code>(.)(?!.{2}\3)(?&lt;!\3.{3})</code></p> <p><strong>Update 2:</strong> in the same way, it is possible to optimize slightly by removing the look-back in the first expression and removing the look-forward in the last, if they need to be unique: pos 1 becomes <code>(.)(?!.{3}\3)</code> and pos 4 becomes <code>(.)(?&lt;!\3.{3})</code></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. 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