Java Scanner - Delimit by spaces unless quotation marks are present?

耗尽温柔 提交于 2020-01-24 11:03:54

问题


I'm trying to use the Scanner class in Java to get data from a configuration file. The file's elements are delimited by whitespace. However, if a phrase or element should be interpreted as a string literal (including whitespace), then double or single-quotes are places around the element. This gives files that look like this:

> R 120 Something AWord

> P 160 SomethingElse "A string literal"

When using the Java Scanner class, it delimits by just whitespace by default. The Scanner class has the useDelimiter() function that takes a regular expression to specify a different delimiter for the text. I'm not good with regular expressions, however, so I'm not sure how I'd do this.

How can I delimit by whitespace, unless there are quotes surrounding something?


回答1:


You can use the scanner.findInLine(pattern) method to specify that you want to keep string literals from being split. You just need a regular expression that will match a quote-less token or one in quotes. This one might work:

"[^\"\\s]+|\"(\\\\.|[^\\\\\"])*\""

(That regex is extra complicated because it handles escapes inside the string literal.)

Example:

String rx = "[^\"\\s]+|\"(\\\\.|[^\\\\\"])*\"";
Scanner scanner = new Scanner("P 160 SomethingElse \"A string literal\" end");
System.out.println(scanner.findInLine(rx)); // => P
System.out.println(scanner.findInLine(rx)); // => 160
System.out.println(scanner.findInLine(rx)); // => SomethingElse
System.out.println(scanner.findInLine(rx)); // => "A string literal"
System.out.println(scanner.findInLine(rx)); // => end

The findInLine method, as the name suggests, only works within the current line. If you want to search the whole input you can use findWithinHorizon instead. You can pass 0 in as the horizon to tell it to use an unlimited horizon:

scanner.findWithinHorizon(rx, 0);


来源:https://stackoverflow.com/questions/12360694/java-scanner-delimit-by-spaces-unless-quotation-marks-are-present

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!