Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, yes there is a crazy regex you can give to String.split:</p> <pre><code>"[^A-Z0-9]+|(?&lt;=[A-Z])(?=[0-9])|(?&lt;=[0-9])(?=[A-Z])" </code></pre> <p>What this means is to split on any sequence of characters which aren't digits or capital letters as well as between any occurrence of a capital letter followed by a digit or any digit followed by a capital letter. The trick here is to match the space between a capital letter and a digit (or vice-versa) without consuming the letter or the digit. For this we use look-behind to match the part before the split and look-ahead to match the part after the split.</p> <p>However as you've probably noticed, the above regex is quite a bit more complicated than your VALID_PATTERN. This is because what you're really doing is trying to extract certain parts from the string, not to split it.</p> <p>So finding all the parts of the string which match the pattern and putting them in a list is the more natural approach to the problem. This is what your code does, but it does so in a needlessly complicated way. You can greatly simplify your code, by simply using Pattern.matcher like this:</p> <pre><code>private static final Pattern VALID_PATTERN = Pattern.compile("[0-9]+|[A-Z]+"); private List&lt;String&gt; parse(String toParse) { List&lt;String&gt; chunks = new LinkedList&lt;String&gt;(); Matcher matcher = VALID_PATTERN.matcher(toParse); while (matcher.find()) { chunks.add( matcher.group() ); } return chunks; } </code></pre> <p>If you do something like this more than once, you might want to refactor the body of this method into a method findAll which takes the string and the pattern as arguments, and then call it as findAll(toParse, VALID_PATTERN) in parse.</p>
    singulars
    1. This table or related slice is empty.
    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