java String.split(regex) design

198 Views Asked by At

I'm importing a file with umpteen lines of "##,##". Each number can be one or two digits.

I'd like to use String.split(regex) to get the two numbers without the adjacent quote marks.

Understanding that I could nibble off the first and last character and use a non-regex split, I'm hoping that there is a regular expression that will make this more graceful.

Suggestions?

EDIT:

In: "12,3"  
Out: 12  
      3
3

There are 3 best solutions below

0
lpiepiora On BEST ANSWER

How about using a regexp \"(d+),(d+)\". Then using Pattern.matcher(input) instead of String.split, and obtaining your digits by Matcher.group(int).

Please consider following snippet:

String line = "\"1,31\"";

Pattern pattern = Pattern.compile("\"(\\d+),(\\d+)\"");
Matcher matcher = pattern.matcher(line);
if (matcher.matches()) {
    int firstNumber = Integer.parseInt(matcher.group(1));
    int secondNumber = Integer.parseInt(matcher.group(2));
    // do whatever with the numbers
}
5
Thomas On

You could split at the quotes as well but that would result in an array of length 4. Unfortunately, there's no way of splitting a string and removing others characters from the same string in one call using String#split.

As an alternative, you could use Apache's StringUtils:

String[] n = StringUtils.removeStart( StringUtils.removeEnd( "##,##", "\""), "\"").split(",");

Edit: as a side note, using StringUtils would allow for missing quotes at the start or end of the input string. If you're sure they're always present, a simple substring(...) might be sufficient. (credits go to @Ingo)

0
araknoid On

You can remove all double-quotes characters in each line then split the string by ,

String toSplit = "\"##,##\"";
String[] splitted = toSplit.replaceAll("\"", "").split(",");

Using \" in the toSplit string to simulate the "##,##" string.