Reputation: 16038
I have text as follows:
"text","1","more, more text","3"
Could anyone kindly show me what regex delimeters I have to use to get the following:
text
1
more, more text
3
I was reading the Sun tutorial here, up until "Methods of the matcher class" but I am still at a loss. Thanks!
If it were something like text,1,more
it would be easy enough, but unfortunately it's not like that. Any ideas?
Upvotes: 0
Views: 9262
Reputation: 3313
You can try something like that. I know, it is not good solution, but it can help you :)
string[] s=text.split("\",\"");
s[0]=s[0].substring(1);
s[s.length-1]=s[s.length-1].substring(0,s[s.length-1].length);
Upvotes: 0
Reputation: 3313
"[^"]*"
seems it is! in java string you can use
"\"[^\"]*\""
You can test it online here: http://www.regexplanet.com/simple/index.html
Upvotes: 0
Reputation: 1475
Try this pattern: "(.+?)"
It will match 1 or more characters between double quotes. The part of the text between the quotes is available as matcher.group(1)
.
Look at the javadoc for Pattern
class to learn more. Also, look at matcher.find()
method.
Upvotes: 3
Reputation: 6322
You can either go straight for the split() method like this:
String text = "\"text\",\"1\",\"more, more text\",\"3\"";
String[] split = text.split("\"(,\")?");
for (String string : split) {
System.out.println(string);
}
(beware that this returns a length 5 array, with the first position being an empty string)
Or, if you want to use a Pattern/Matcher, you can do it like this:
Pattern pattern = Pattern.compile("\"([^\"]+)\"");
Matcher matcher = pattern.matcher(text);
while(matcher.find()){
System.out.println(matcher.group(1));
}
Upvotes: 1
Reputation: 14053
You could get every part that start with an "
and finish with another "
and then substring the first and last character of each part.
Upvotes: 0