Nate
Nate

Reputation: 155

Trouble splitting string with split(regex) Java

I want to split a number of strings similar to name: john, id: 20, dest: toledo, from: seattle, date_time: [2/8/12 15:48:01:837 MST] into only these tokens:

john
20
toledo
seattle
[2/8/12 15:48:01:837 MST]

I'm doing this

String delims = "(name|id|dest|from|date_time)?[:,\\s]+";
String line = "name: john, id: 20, dest: toledo, from: seattle, date_time: [2/8/12 15:48:01:837 MST]";
String[] lineTokens = line.split(delims, 5);

for (String t : lineTokens)
{
    // for debugging
    System.out.println (t);
    // other processing I want to do
}   

but every even element in lineTokens turns out to be either empty or just whitespace. Each odd element in lineTokens is what I want, i.e. lineTokens[0] is "", lineTokens[1] is "john", lineTokens[2] is "", lineTokens[3] is "20", etc. Can anyone explain what I'm doing wrong?

Upvotes: 2

Views: 153

Answers (3)

RanRag
RanRag

Reputation: 49597

Why not a little less complicated regex solution.

String str =  "name: john, id: 20, dest: toledo, from: seattle, date_time: [2/8/12 15:48:01:837 MST]";
String[] expr = str.split(", ");
for(String e : expr)
System.out.println(e.split(": ")[1]);

Output =

john

20

toledo

seattle

[2/8/12 15:48:01:837 MST]

Upvotes: 2

user1227804
user1227804

Reputation: 390

I made some changes to your code:

    String delims = "(name|id|dest|from|date_time)[:,\\s]+";
    String line = "name: john, id: 20, dest: toledo, from: seattle, date_time: [2/8/12 15:48:01:837 MST]";
    String[] lineTokens = line.split(delims);

    for (String t : lineTokens)
    {
        // for debugging
        System.out.println (t);
        // other processing I want to do
    }   

also you should ignore the first element in lineTokens, since it's the capturing from the beginning of the line till "name:...."

Upvotes: 1

James Montagne
James Montagne

Reputation: 78770

The problem is that your regex is not matching , id: as a whole, it is matching , as one and then id: as a 2nd match. Between these two matches you have an empty string. You need to modify it to match the whole thing. Something like this:

String delims = "(, )?(name|id|dest|from|date_time)?[:\\s]+";

http://ideone.com/Qgs8y

Upvotes: 3

Related Questions