0

我正在使用 Java 正则表达式。我有一个字符串如下

String s = "the location is at {emp.address.street} with the name {emp.name},{emp.surname}";

对于上述字符串,s.replaceAll( "\\{(.*?)\\}", "" )返回以下字符串:

the location is at with the name ,

现在我想将其反转以获得以下结果:

{emp.address.street}{emp.name}{emp.surname}

4

2 回答 2

1

这个经过测试的脚本对我有用:

public class TEST
{
    public static void main( String[] args )
    {
        String s = "the location is at {emp.address.street} with the name {emp.name},{emp.surname}";
        String result = s.replaceAll( "[^{}]+|(\\{(.*?)\\})", "$1" );
        System.out.println(result);
    }
}
于 2011-10-11T03:34:58.967 回答
0

您可以创建一个Pattern并使用Matcher.find()Matcher.group()获取该模式的一部分。以下是这些类的 Javadocs:Matcher javadoc Pattern javadoc

于 2011-10-11T03:27:31.647 回答