Alex
Alex

Reputation: 751

How do I parse this string easily?

So I want to extract data from something on the form:

"San Diego"--"San Antonio" [1319]

I want to extract both of the cities names and the number at the end in brackets[]. If the city is one word there is no "" like in this example:

Toledo--Springfield [677]

Right now I'm using str.split("--") and then str.split(" ["). However the latter is not poosible and if I try to use split(" ") it will split in the middle of two word cities.

Upvotes: 0

Views: 119

Answers (3)

Onur A.
Onur A.

Reputation: 3027

you can also use StringTokenizer in alternative to regex, in StringTokenizer you can specify delimiters next to each other, here assume you want -- and [ as delimiters, so just write --[ in delimiter section

String input="\"San Diego\"--\"San Antonio\" [1319]";
StringTokenizer tok = new StringTokenizer(input, "--[");
while (tok.hasMoreTokens()) 
{
    System.out.println(tok.nextToken());
}

Upvotes: 1

sp00m
sp00m

Reputation: 48837

A regex seems to be appropriated:

"?(.*?)"?--"?(.*?)"? \[(.*?)]

In Java:

Pattern p = Pattern.compile("\"?(.*?)\"?--\"?(.*?)\"? \\[(.*?)]");
Matcher m = p.matcher("\"San Diego\"--\"San Antonio\" [1319]");
if (m.find()) {
    System.out.println(m.group(1));
    System.out.println(m.group(2));
    System.out.println(m.group(3));
}

Prints:

San Diego
San Antonio
1319

Regexp explanation

  • "? means "a " or not"
  • .*? means "any string"
  • the parentheses capture the data between them, that's why afterwards, we can access them with m.group(...).

Upvotes: 6

Kanagaraj M
Kanagaraj M

Reputation: 966

After split by -- then check by using contains() method for " if it is true then split by space and [ .

Upvotes: 1

Related Questions