简体   繁体   English

使用正则表达式获取重复模式

[英]Get repeating pattern using regex

If I have this input string: {post:[matt]} 如果我有这个输入字符串:{post:[matt]}

and I want to get the string where "matt" currently is, I'd use this: 我想得到当前“matt”的字符串,我会用它:

Pattern pattern = 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]} 但如果我有这样的字符串:{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? 我怎么能得到字符串“matt”,“13-mar-2011”和“hello” - 当它们可能有多少变量? If it was just 3 it would be easy. 如果只有3则很容易。

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() matcher.find()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM