Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It's much easier to use a <a href="http://java.sun.com/javase/6/docs/api/java/util/regex/Matcher.html" rel="noreferrer"><code>java.util.regex.Matcher</code></a> and do a <code>find()</code> rather than any kind of <code>split</code> in these kinds of scenario.</p> <p>That is, instead of defining the pattern for the <em>delimiter</em> between the tokens, you define the pattern for <em>the tokens themselves</em>.</p> <p>Here's an example:</p> <pre><code> String text = "1 2 \"333 4\" 55 6 \"77\" 8 999"; // 1 2 "333 4" 55 6 "77" 8 999 String regex = "\"([^\"]*)\"|(\\S+)"; Matcher m = Pattern.compile(regex).matcher(text); while (m.find()) { if (m.group(1) != null) { System.out.println("Quoted [" + m.group(1) + "]"); } else { System.out.println("Plain [" + m.group(2) + "]"); } } </code></pre> <p>The above prints (<a href="http://ideone.com/OXsUp" rel="noreferrer">as seen on ideone.com</a>):</p> <pre><code>Plain [1] Plain [2] Quoted [333 4] Plain [55] Plain [6] Quoted [77] Plain [8] Plain [999] </code></pre> <p>The pattern is essentially:</p> <pre><code>"([^"]*)"|(\S+) \_____/ \___/ 1 2 </code></pre> <p>There are 2 alternates:</p> <ul> <li>The first alternate matches the opening double quote, a sequence of anything but double quote (captured in group 1), then the closing double quote</li> <li>The second alternate matches any sequence of non-whitespace characters, captured in group 2</li> <li>The order of the alternates matter in this pattern</li> </ul> <p>Note that this does not handle escaped double quotes within quoted segments. If you need to do this, then the pattern becomes more complicated, but the <code>Matcher</code> solution still works.</p> <h3>References</h3> <ul> <li><a href="http://www.regular-expressions.info/brackets.html" rel="noreferrer">regular-expressions.info/Brackets for Grouping and Capturing</a>, <a href="http://www.regular-expressions.info/alternation.html" rel="noreferrer">Alternation with Vertical Bar</a>, <a href="http://www.regular-expressions.info/charclass.html" rel="noreferrer">Character Class</a>, <a href="http://www.regular-expressions.info/repeat.html" rel="noreferrer">Repetition with Star and Plus</a></li> </ul> <h3>See also</h3> <ul> <li><a href="http://www.regular-expressions.info/examplesprogrammer.html" rel="noreferrer">regular-expressions.info/Examples - Programmer - Strings</a> - for pattern with escaped quotes</li> </ul> <hr> <h3>Appendix</h3> <p>Note that <a href="http://java.sun.com/javase/6/docs/api/java/util/StringTokenizer.html" rel="noreferrer"><code>StringTokenizer</code></a> is a <em>legacy class</em>. It's recommended to use <a href="http://java.sun.com/javase/6/docs/api/java/util/Scanner.html" rel="noreferrer"><code>java.util.Scanner</code></a> or <a href="http://download.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29" rel="noreferrer"><code>String.split</code></a>, or of course <a href="http://java.sun.com/javase/6/docs/api/java/util/regex/Matcher.html" rel="noreferrer"><code>java.util.regex.Matcher</code></a> for most flexibility.</p> <h3>Related questions</h3> <ul> <li><a href="https://stackoverflow.com/questions/2873254/difference-between-a-deprecated-and-legacy-api">Difference between a Deprecated and Legacy API?</a></li> <li><a href="https://stackoverflow.com/questions/691184/scanner-vs-stringtokenizer-vs-string-split">Scanner vs. StringTokenizer vs. String.Split</a></li> <li><a href="https://stackoverflow.com/questions/3059333/validating-input-using-java-util-scanner/">Validating input using java.util.Scanner</a> - has many examples</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