How to negate inverse a regex expression
I am using Java regex. I have a String as follow
String s = "the location is at {emp.address.street} with the name {emp开发者_JAVA技巧.name},{emp.surname}";
For above string, s.replaceAll( "\\{(.*?)\\}", "" )
returns the following string:
the location is at with the name ,
Now I want to inverse this to get following result:
{emp.address.street}{emp.name}{emp.surname}
This tested script works for me:
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);
}
}
You can create a Pattern
and use Matcher.find()
and Matcher.group()
to get parts of that pattern.
Here's the Javadocs for the classes: Matcher javadoc Pattern javadoc
精彩评论