簡體   English   中英

如何取反正則表達式

[英]How to negate inverse a regex expression

我正在使用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()來獲取該模式的一部分。 這是這些類的Javadocs: 匹配器javadoc 模式javadoc

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM