开发者

Get repeating pattern using regex

开发者 https://www.devze.com 2023-02-16 09:20 出处:网络
If I have this input string: {post:[matt]} and I want to get the string where \"matt\" currently is, I\'d use this:

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()

0

精彩评论

暂无评论...
验证码 换一张
取 消