Reemplace la última aparición de palabra entre 2 palabras con RegEx

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 entre dos ORDERs and replace them with word QQQ.

He aquí algunos ejemplos:

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"

preguntado el 28 de mayo de 14 a las 11:05

1 Respuestas

prueba este

    s = s.replaceAll("(ORDER.*ASC)(?=.*?ORDER.*)", "QQQ");

contestado el 28 de mayo de 14 a las 12:05

what to do for remove last "ORDER" too? - user2761286

@user2761286 State clearly in question what is the intended result. Currently you have ORDER in the output. - Ulugbek Umírov

what if i want to remove that ORDER too? - user2761286

s.replaceAll("(ORDER.*ORDER)", "QQQ"); will replace ORDER...ORDER with QQQ - Evgeniy Dorofeev

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.