Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 基于模式拆分字符串的正则表达式_Java_Regex_String - Fatal编程技术网

Java 基于模式拆分字符串的正则表达式

Java 基于模式拆分字符串的正则表达式,java,regex,string,Java,Regex,String,我希望有人帮助我更正正则表达式以拆分此字符串: {constraint.null.invalid}{0,1,2} 基本上,我想要{和}中的任何内容,因此我的输出必须是: constraint.null.无效 0,1,2 我仔细尝试过的正则表达式是: \{([\S]+)\} 但我得到的价值是: constraint.null.invalid}{0,1,2 我错过了什么 示例代码: public static void main(String[] args) { Pattern p

我希望有人帮助我更正正则表达式以拆分此字符串:

{constraint.null.invalid}{0,1,2}
基本上,我想要
{
}
中的任何内容,因此我的输出必须是:

  • constraint.null.无效
  • 0,1,2
我仔细尝试过的正则表达式是:

\{([\S]+)\}
但我得到的价值是:

constraint.null.invalid}{0,1,2
我错过了什么

示例代码:

public static void main(String[] args) {
    Pattern pattern = Pattern.compile("\\{([\\S]+)\\}", Pattern.MULTILINE);
    String test = "{constraint.null.invalid}{0,1,2}";
    Matcher matcher = pattern.matcher(test);
    while (matcher.find()) {
        System.out.println(matcher.group(1));
    }
}
谢谢



PS:字符串可以包含由1个或多个
{
}
限定的值
+
量词是贪婪的。对不情愿的版本使用
+?


有关详细信息,请参见。

量词是贪婪的。对不情愿的版本使用
+?


有关详细信息,请参阅。

一种稍微不同的方法,使用此模式“\{([^\{}]*)\}”


一种稍微不同的方法,使用这种模式“\{([^\{\}]*)\}”

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ReTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String s = "bla bla {constraint.null.invalid} bla bla bla {0,1,2} bla bla";
        Pattern p = Pattern.compile("\\{([^\\{\\}]*)\\}");

        Matcher m = p.matcher(s);

        while (m.find()){
            System.out.println(m.group(1));
        }
    }
}