首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将括号中的所有文本替换为Java

将括号中的所有文本替换为Java
EN

Stack Overflow用户
提问于 2022-07-08 08:50:17
回答 1查看 47关注 0票数 0

我有绳子

代码语言:javascript
复制
String test = "
    test_one {
    Lorem Ipsum is simply dummy text of the printing and typesetting industry.
    }
    
    test_two {
    Lorem Ipsum is simply dummy text of the printing and typesetting industry.
    }
    
    test_three {
    Lorem Ipsum is simply dummy text of the printing and typesetting industry.
    }
"

我需要将'test_two‘或'test_three’中括号中的整个文本替换为另一个文本ex“这是我的文本”;

预期产出:

代码语言:javascript
复制
String test = "
        test_one {
        Lorem Ipsum is simply dummy text of the printing and typesetting industry.
        }
        
        test_two {
        Lorem Ipsum is simply dummy text of the printing and typesetting industry.
        }
        
        test_three {
        **This is my text**
        }
    "
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-08 09:03:51

我们可以用启用了点所有模式的regex替换:

代码语言:javascript
复制
String test = "test_one {\nLorem Ipsum is simply dummy text of the printing and typesetting industry.\n}\ntest_two {\nLorem Ipsum is simply dummy text of the printing and typesetting industry.\n}\n\ntest_three {\nLorem Ipsum is simply dummy text of the printing and typesetting industry.\n}";
test = test.replaceAll("(?s)\\btest_three \\{.*?\\}", "test_three {\nThis is my text\n}");
System.out.println(test);

这些指纹:

代码语言:javascript
复制
test_one {
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
}
test_two {
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
}

test_three {
This is my text
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72908935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档