简体   繁体   English

如何在嵌套字符串中使用正则表达式替换?

[英]How to replace using regular expression in nested string?

I have this: start_xxx_end_Leftstart_xxx_end_Right 我有这个: start_xxx_end_Leftstart_xxx_end_Right

How can I use the regular expression to remove characters between start and end (inclusive) so that I can get the following string: 如何使用正则表达式删除startend (包括)之间的字符,以便获得以下字符串:

_Left_Right

I tried this regex in java but it removes EVERYTHING between start and end : 我在Java中尝试过此正则表达式,但它删除了startend之间的所有内容:

start(.*)end

Just use replaceAll method to replace the substring from start to end : - 只要使用replaceAll方法来代替子从startend : -

String str = "start_xxx_end_Leftstart_xxx_end_Right";
str = str.replaceAll("start.*?end", "");
System.out.println(str);

OUTPUT : - 输出:-

"_Left_Right"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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