简体   繁体   中英

Replace String using regular expressions

Replacement with Regular expressions in Java - I have a String

allof{
    condition {licenseState=="NY"}
    condition{professionId==301}
    condition  {professionId=="301"}
}

would be it's possible to do a replacement in java using regex so the string looks like this one:

allof{
    condition 'licenseState=="NY"', {licenseState=="NY"}
    condition 'professionId==301', {professionId==301}
    condition 'professionId=="301"', {professionId=="301"}
    }

basically getting what inside the {} brackets and putting it separately. Is it possible and how? NewLine char is not guaranteed to be present after each condition.

I've tried:

condition\s*?{[a-zA-Z0-9=<>\s"']*} 

You can use the string replaceAll function. Something like this:

yourString.replaceAll("\\{([^{}]*\\=\\=[^{}]*)\\}", "'$1', {$1}");

假设没有嵌套{}

str = str.replaceAll( "(?<=condition)\\s*(\\{([^}]+)\\})", " '$2', $1" );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM