简体   繁体   中英

How to replace using regular expression in nested string?

I have this: 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:

_Left_Right

I tried this regex in java but it removes EVERYTHING between start and end :

start(.*)end

Just use replaceAll method to replace the substring from start to end : -

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

OUTPUT : -

"_Left_Right"

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