我使用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}
经过测试的脚本适合我:
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);
}
}
您可以创建Pattern
并使用Matcher.find()
和Matcher.group()
来获取该模式的部分内容。下面是类的javadoc: Matcher javadoc Pattern javadoc