Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In order to do such a thing, you could use negative look ahead before matching a new group.</p> <p>I will use a more general PCRE notation:</p> <pre><code>(.)((?!\1).)((?!\1|\2).)\3\2((?!\1\2\3).)\2\1 </code></pre> <p>The regex above will match the string <code>12332421</code> but will not match <code>12112421</code> or <code>11111111</code>.</p> <p>A short explanation:</p> <pre><code>(.) // match any character (except line breaks) and store it in group 1 ( // open group 2 (?!\1) // if looking ahead group 1 cannot be seen, . // match any character (except line breaks) ) // close group 2 ( // open group 3 (?!\1|\2) // if looking ahead group 1 or 2 cannot be seen, . // match any character (except line breaks) ) // close group 3 \3 // back referencing group 3 \2 // back referencing group 2 ( // open group 4 (?!\1\2\3) // if looking ahead group 1, 2 or 3 cannot be seen, . // match any character (except line breaks) ) // close group 4 \2 // back referencing group 2 \1 // back referencing group 1 </code></pre> <p>Of course, you don't really need to group #4 since you're not back referencing it.</p> <p>You might agree with me that regex is not be the best tool to do this kind of matching...</p> <p><strong>Edit</strong>:</p> <p>Well, I don't see how you're going to build these regexes, but can't imagine it being easier than this little method which simply accepts a pattern and a target string and tests if they match:</p> <pre><code>public class Test { public static boolean matchesPattern(String text, String pattern) { if(text.length() != pattern.length()) return false; Map&lt;Character, Character&gt; mappings = new HashMap&lt;Character, Character&gt;(); for(int index = 0; index &lt; pattern.length(); index++) { Character patternChar = pattern.charAt(index); Character textChar = text.charAt(index); if(mappings.containsKey(patternChar)) { if(mappings.get(patternChar) != textChar) return false; } else { if(mappings.values().contains(textChar)) return false; mappings.put(patternChar, textChar); } } return true; } public static void main(String[] args) { String pattern = "abccbdba"; String[] tests = {"12332421", "12112421", "11111111"}; for(String t : tests) { System.out.println(t+" -&gt; "+matchesPattern(t, pattern)); } } } </code></pre> <p>which produces the following output:</p> <pre><code>12332421 -&gt; true 12112421 -&gt; false 11111111 -&gt; false </code></pre>
    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.
 

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