gpt4 book ai didi

java - 用 RegEx 替换 2 个单词之间最后出现的单词

转载 作者:行者123 更新时间:2023-12-01 12:55:50 26 4
gpt4 key购买 nike

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

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

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

我必须找到每个子字符串,直到两个 ORDER 之间最后一次出现 ASC 为止,并将它们替换为单词 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"

最佳答案

试试这个

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

关于java - 用 RegEx 替换 2 个单词之间最后出现的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23909689/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com