title: How to split string in words and non-words
body: <p>I am looking for an elegant way of splitting a string in words and non-words, where a "word" is defined by some regular expression (for instance, <code>[a-zA-Z]+</code>).</p>
  
  <p>Input is a string, output should be a list of word and non-word substrings in order. For instance:</p>
  
  <pre><code>"A! B C, d." -&gt; Arrays.asList("A", "! ", "B", " ", "C", ", ","d", ".")
  </code></pre>
  
  <p>Here's my take:</p>
  
  <pre><code>public static String WORD_PATTERN = "[a-zA-Z]+";
  
  public static List&lt;String&gt; splitString(String str) {
      if (str == null) {
          return null;
      }
      Pattern wordPattern = Pattern.compile(WORD_PATTERN);
      Matcher wordMatcher = wordPattern.matcher(str);
  
      List&lt;String&gt; splitString = new ArrayList&lt;&gt;();
  
      int endOfLastWord = 0;
  
      while(wordMatcher.find())
      {
          int startOfNextWord = wordMatcher.start();
          int endOfNextWord = wordMatcher.end();
  
          if (startOfNextWord &gt; endOfLastWord) {
              String nextNonWord = str.substring(endOfLastWord, startOfNextWord);
              splitString.add(nextNonWord);
          }
  
          String nextWord = str.substring(startOfNextWord, endOfNextWord);
          splitString.add(nextWord);
          endOfLastWord = endOfNextWord;
      }
  
      if (endOfLastWord &lt; str.length()) {
          String lastNonWord = str.substring(endOfLastWord);
          splitString.add(lastNonWord);
      }
      return splitString;
  }
  </code></pre>
  
  <p>This does not feel elegant, I think there should be a better way which I'm just not aware of.</p>
  
  <p>I am not looking to improve the code above, so please don't refer to <a href="https://codereview.stackexchange.com/">Codereview</a>. I've only posted it to avoid "what have you tried so far" comments.</p>
  
  <p>I am looking for a more concise and elegant way, ideally only using standard Java packages.</p>