Get repeating pattern using regex
If I have this input string: {post:[matt]}
and I want to get the string where "matt" currently is, I'd use this:
Pattern patte开发者_C百科rn = Pattern.compile("^\\{(.+):[(.+)]\\}$")
Matcher matcher = pattern.matcher("{post:[matt]}");
if(matcher.matches()) {
// pattern matches input string
String str1 = matcher.group(2);
But if I had a string like this: {post:[matt,13-mar-2011,hello]}
how would I get the strings "matt", "13-mar-2011" and "hello" - when there may be a variable number of them? If it was just 3 it would be easy.
You can try the find function, its supposed to find next match and next and next... I dont know if you will have to modify your pattern or not.
matcher.find()
精彩评论