Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can do it like this:</p> <pre><code>[ab] [xy]|[xy] [ab]|[abxy] </code></pre> <p>There are 3 choices here:</p> <ul> <li>Only <code>a</code>, <code>b</code>, <code>x</code>, <code>y</code> (single character)</li> <li>Or 2 character, <code>a</code> or <code>b</code> comes before <code>x</code> or <code>y</code>, space in between.</li> <li>Or 2 character, <code>x</code> or <code>y</code> comes before <code>a</code> or <code>b</code>, space in between.</li> </ul> <p>I put <code>[abxy]</code> behind, just in case when you search, it will search for those in front (the paired up ones) before searching for the single ones. The order is important if you use the regex to search, but it doesn't matter much when you do validation.</p> <p>Another way to write it:</p> <pre><code>[ab]( [xy])?|[xy]( [ab])? </code></pre> <p>That only works for character, but you can make it works for string. For example, let's say you have 4 strings <code>s1</code>, <code>s2</code>, <code>s3</code>, <code>s4</code>:</p> <pre><code>(s1|s2)( (s3|s4))?|(s3|s4)( (s1|s2))? </code></pre> <p>It searches for:</p> <ul> <li>Either <code>s1</code> or <code>s2</code>, may or may not (0 or 1 instance of) followed by <code>s3</code> or <code>s4</code></li> <li>(The other way around)</li> </ul> <p>This covers all the cases of <code>s1</code>, <code>s2</code>, etc. (single string), <code>s2 s3</code>, <code>s3 s2</code>, etc. (paired up, can reverse the order). The regex above will search for the longer version (paired up) before resorting to the single string, due to the default <em>greedy</em> property of quantifiers.</p> <p>Note that I am using <em>capturing groups</em> <code>(pattern)</code> in the regex above, which will record the position of the string that matches the <code>pattern</code> inside. You can make them non-capturing group <code>(?:pattern)</code>, if you don't need to refer to the text that matches the pattern. This will save you some clock cycles.</p> <pre><code>(?:s1|s2)(?: (?:s3|s4))?|(?:s3|s4)(?: (?:s1|s2))? </code></pre> <p>(I leave the task of changing <em>capturing group</em> to <em>non-capturing group</em> for the other regex as an exercise. It is as simple as adding <code>?:</code>)</p> <p><strong>Searching or Validation?</strong></p> <p>If you want to find such pattern, then the regex above should work for you.</p> <p>If you want to validate that the string matches the pattern, you need to use anchors <code>^</code> (match beginning of string), <code>$</code> (match the end of the string) to make sure the string follows the exact format:</p> <pre><code>^([ab] [xy]|[xy] [ab]|[abxy])$ ^([ab]( [xy])?|[xy]( [ab])?)$ ^((s1|s2)( (s3|s4))?|(s3|s4)( (s1|s2))?)$ ^(?:(?:s1|s2)(?: (?:s3|s4))?|(?:s3|s4)(?: (?:s1|s2))?)$ </code></pre> <p>Note that I surround the regex from the above sections with <code>()</code> (<em>capturing group</em>, but I only need <em>grouping</em> here actually). This is because I have an alternation <code>|</code> inside.</p> <p><strong>Extensibility and Limitations</strong></p> <ul> <li><p>You can add more strings to either the first group or second group as you like:</p> <pre><code>^([abcd]( [xyz])?|[xyz]( [abcd])?)$ </code></pre></li> <li><p>However, if you want to increase the number of groups, I suggest that you split the string by spaces and loop through the tokens to find the permutations of the group, rather than using regex.</p></li> </ul>
 

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