Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A quick, dirty, and almost-not-pseudo-code answer:</p> <pre><code>StringBuilder sb = new StringBuilder("["); Set&lt;Character&gt; metaChars = //...appropriate initialization while (sourceString.length() != 0) { char c = sourceString.charAt(0); sb.append(metaChars.contains(c) ? "\\"+c : c); sourceString.replace(c,''); } sb.append("]"); Pattern p = Pattern.compile(sb.toString()); //...can check here for the appropriate sb.length cases // e.g, 2 = empty, all chars equals the count of whatever set qualifies as all chars, etc </code></pre> <p>Which gives you the unique string of char's you want to match, with <a href="http://download.oracle.com/javase/tutorial/essential/regex/literals.html" rel="nofollow">meta-characters</a> replaced. It will not convert things into ranges (which I think is fine - doing so smells like premature optimization to me). You can do some post tests for <em>simple</em> set cases - like matching <code>sb</code> against digits, non-digits, etc, but unless you know that's going to buy you a lot of performance (or the simplification is the point of this program), I wouldn't bother.</p> <p>If you really want to do ranges, you could instead <code>sourceString.toCharArray()</code>, sort that, iterate deleting repetitions and doing some sort of range check and replacing meta characters as you add the contents to <code>StringBuilder</code>.</p> <p>EDIT: I actually kind of liked the <code>toCharArray</code> version, so pseudo-coded it out as well:</p> <pre><code>//...check for empty here, if not... char[] sourceC = sourceString.toCharArray(); Arrays.sort(sourceC); lastC = sourceC[0]; StringBuilder sb = new StringBuilder("["); StringBuilder range = new StringBuilder(); for (int i=1; i&lt;sourceC.length; i++) { if (lastC == sourceC[i]) continue; if (//.. next char in sequence..//) //..add to range else { // check range size, append accordingly to sb as a single item, range, etc } lastC = sourceC[i]; } </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. 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