java - 使用正则表达式替换字符串

标签 java regex text replace

用 Java 中的正则表达式替换 - 我有一个字符串


    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"']*} 

最佳答案

您可以使用string replaceAll功能。像这样的事情:

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

关于java - 使用正则表达式替换字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15709548/

相关文章:

java - Android:让应用程序使用共享首选项提醒答案

java - 挥杆:对齐问题

regex - 如何在 find 命令上使用正则表达式作为名称?

C++ 读取长的非分隔整数,将它们分开并放入二维数组中

C 请求用户txt文件,保存在数组中,并以txt输出

java - JUnit 测试对象的静态计数器

java - 除 IoC 和依赖注入(inject)之外的类和项目中的解耦 (java)

java - 正则表达式:删除第二个 "-"之前和之后的所有内容

asp.net - 用于验证下拉条目的正则表达式

javascript - 使用 JS 或 jQuery 写入文件