Reputation: 389
I currently have this string:
"display_name":"test","game":"test123"
and I want to split the string so I can get the value test
. I have looked all over the internet and tried some things, but I couldn't get it to work.
I found that splitting using quotation marks could be done using this regex: \"([^\"]*)\"
. So I tried this regex: display_name:\":\"([^\"]*)\"game\"
, but this returned null. I hope that someone could explain me why my regex didn't work and how it should be done.
Upvotes: 0
Views: 168
Reputation:
I think you could do it easier, like this:
/(\w)+/g
This little regex will take all your strings.
Your java code should be something like:
Pattern pattern = Pattern.compile("(\w)+");
Matcher matcher = pattern.matcher(yourText);
while (matcher.find()) {
System.out.println("Result: " + matcher.group(2));
}
I also want to note as @AbishekManoharan noted that it looks like JSON
Upvotes: 0
Reputation: 174706
You forget to include the "
,comma before "game"
and also you need to remove the extra colon after display_name
display_name\":\"([^\"]*)\",\"game\"
or
\"display_name\":\"([^\"]*)\",\"game\"
Now, print the group index 1.
Matcher m = Pattern.compile("\"display_name\":\"([^\"]*)\",\"game\"").matcher(str);
while(m.find())
{
System.out.println(m.group(1))
}
Upvotes: 1