![]() |
The Java Developers Almanac 1.4Order this book from Amazon. |
e431. Parsing Character-Separated Data with a Regular ExpressionA line from a flat-file is typically formatted using a separator character to separate the fields. If the separator is simply a comma, tab, or single character, theStringTokenizer class can be used to
parse the line into fields. If the separator is more complex (e.g., a
space after a comma), a regular expression is needed.
String.split() conveniently parses a line using a regular
expression to specify the separator.
Note: The // Parse a comma-separated string String inputStr = "a,,b"; String patternStr = ","; String[] fields = inputStr.split(patternStr); // ["a", "", "b"] // Parse a line whose separator is a comma followed by a space inputStr = "a, b, c,d"; patternStr = ", "; fields = inputStr.split(patternStr, -1); // ["a", "b", "c,d"] // Parse a line with and's and or's inputStr = "a, b, and c"; patternStr = "[, ]+(and|or)*[, ]*"; fields = inputStr.split(patternStr, -1); // ["a", "b", "c"]
© 2002 Addison-Wesley. |