0

I'm working with RegEx on Java and here is where I stuck.

I have a simple string like

String sample1 = "word 231 sadas aslkjasf anything ORDER what whut ASC hello ORDER whatever."

I have to find every substring until the last occurrence of ASC between two ORDERs and replace them with word QQQ.

Here are some examples:

String sample1 = "word 231 sadas aslkjasf anything ORDER what whut ASC hello ORDER whatever.";
String result1 = "word 231 sadas aslkjasf anything QQQ hello ORDER whatever.";
// replaced "ORDER what whut ASC"

String sample2 = "word 111 sadas anything ORDER what whut yeap ASC nope ASC maybe ORDER yeah.";
String result2 = "word 111 sadas anything QQQ maybe ORDER yeah.";
// replaced "ORDER what whut yeap ASC nope ASC"

1 Answer 1

1

try this

    s = s.replaceAll("(ORDER.*ASC)(?=.*?ORDER.*)", "QQQ");
4
  • what to do for remove last "ORDER" too? May 28, 2014 at 11:09
  • @user2761286 State clearly in question what is the intended result. Currently you have ORDER in the output. May 28, 2014 at 11:11
  • what if i want to remove that ORDER too? May 28, 2014 at 11:28
  • s.replaceAll("(ORDER.*ORDER)", "QQQ"); will replace ORDER...ORDER with QQQ May 28, 2014 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.