开发者

How to negate inverse a regex expression

开发者 https://www.devze.com 2023-04-12 16:04 出处:网络
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}\";

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号