用RegEx替换2个单词之间的最后一个单词

时间:2014-05-28 10:54:45

标签: java regex string

我正在使用RegEx on Java,这就是我遇到的问题。

我有一个简单的字符串,如

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

我必须找到每个子字符串,直到两个ASC之间最后一次出现ORDER,并用字QQQ替换它们。

以下是一些例子:

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 个答案:

答案 0 :(得分:1)

试试这个

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