0

I'm trying to split the following string primarily based on whitespace. Please refer to the following example

Name:"John Adam"  languge:"english"  Date:" August 2011"

I need to split the text based on each parameter. For e.g.

Name:"John Adam"

languge:"english"

Date:" August 2011"

I'm not able to construct the right regex for this scenario.

Any pointers will be appreciated.

-Thanks

DwB
  • 37,124
  • 11
  • 56
  • 82
Shamik
  • 1,671
  • 11
  • 36
  • 64
  • Is the `languge"english"` a mistake or thats how it is? – Mob Feb 03 '12 at 19:33
  • there's no `:` between languge and "english" ? – driangle Feb 03 '12 at 19:33
  • 1
    Possible duplicate/might be helpful to look at http://stackoverflow.com/questions/225337/how-do-i-split-a-string-with-any-whitespace-chars-as-delimiters – Aurora Feb 03 '12 at 19:33
  • Sorry, its a typo. I've fixed it now. @Aurora .. The reference you've given splits based on whitespace. In my case, it won't work since it'll split "John" and "Adam" as well. – Shamik Feb 03 '12 at 19:35
  • [Java Regex Test Applet](http://www.cis.upenn.edu/~matuszek/General/RegexTester/regex-tester.html) is a good tool for such stuff. – DwB Feb 03 '12 at 19:45

5 Answers5

4
    String input = "Name:\"John Adam\"  languge:\"english\"  Date:\" August 2011\"";
    // You can define this pattern statically.
    Pattern pattern = Pattern.compile("(.*?\\:\\\".*?\\\")\\s*");
    Matcher matcher = pattern.matcher(input);
    List<String> keyValues = new LinkedList<String>();
    while(matcher.find()){
        keyValues.add(matcher.group());
    }

    //keyValues == [Name:"John Adam"  , languge:"english"  , Date:" August 2011"]
driangle
  • 11,601
  • 5
  • 47
  • 54
1

you can use the class StringTokenizer.. so to split something with whitespaces you could do something like:

String name=" Hello world 2011";
    StringTokenizer tokens=new StringTokenizer(name);
    while(tokens.hasMoreTokens()){
            System.out.println(tokens.nextToken());
        }

and that should split it to:

Hello
world
2011

this little tutorial could help you: http://www.cstutoringcenter.com/tutorials/java/java5.php

Kevin K
  • 9,344
  • 3
  • 37
  • 62
Raykud
  • 2,488
  • 3
  • 21
  • 41
0

I would look first at using String.split() in two passes: the first splitting on a space character, the second on double-quotes.

oldingn
  • 86
  • 4
  • This won't work. I think you mean the other way around? Clearly splitting on spaces first will give you the token `Name:"John`, which is not intentional. – stefan Feb 03 '12 at 19:37
  • @stefan ... you are right, the whitespace splitting won't work in my case. – Shamik Feb 03 '12 at 19:38
0

It might be simpler not to use a regular expression. Just have a loop that looks for a colon, then for a double-quote, then another double-quote, then for whitespace. (OK, use a regular expression for the whitespace bit). As the loop proceeds, you'll get a String for the key, and a String for the value. You'd break the loop as soon as you fail to find the character that you're looking for.

Comment on this answer if it's not clear how to do this, and I'll post some code. But I think I've given you enough to get started.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
0
String ourStr = "Name:\"John Adam\"  languge:\"english\"  Date:\" August 2011\"";

String[] newStr = ourStr.split(" ");

for(int i=0;i<newStr.length;i++) {
   System.out.println(newStr[i] + "\n");
}

Output:

Name:"John Adam"

languge:"english"

Date:" August 2011"
El3ctr0n1c4
  • 400
  • 2
  • 7
  • 18