简体   繁体   中英

Regex to dynamically replace text in java

What I am trying to do is make something like:

some random stuff substitute("random stuff","xxx") test

be replaced to the following:

some xxx test

If I use:

substitute\((.*?)*\)

I get to find the portion, but what I ultimately want is multiple groups where the first group is the text to search and the second group is to replace. I want it to be generic enough so I dont depend on the , since it can appear anywhere. Is there a regex that could work for all cases or should I be depending on the "" to get what I need?

Don't have tested it but you could try

\w*\(\"(.*)\".*\"(.*)\"\)

If order in substitute doesn't change you have in your first group the search and in second the replace term. And yes I don't see another way but depending on () and "".

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