gpt4 book ai didi

java - 正则表达式:在段落中出现任何字符串列表后删除任何单词中的后缀字符串

转载 作者:行者123 更新时间:2023-11-29 04:07:23 27 4
gpt4 key购买 nike

我有一个更大的字符串和一个字符串列表。我想改变更大的字符串,这样- 对于更大字符串中列表中出现的任何字符串,删除后缀部分直到下一个空格。

Bigger String

WITH dataTab0 AS (SELECT TO_CHAR(to_date(tab_0_0.times),'YYYYMMDD')  AS TIME_ID_CATEGORYe93bc60a0041,tab_0_0.request_id AS PAGE_IMPRESSIONf6beefc4b44e4b  FROM full_contents_2

List

TIME_ID_CATEGORY
PAGE_IMPRESSION
...

我需要删除 TIME_ID_CATEGORY 和 PAGE_IMPRESSION 之后的后缀,例如 e93bc60a0041 和 f6beefc4b44e4b

我期待以下结果。我需要在 Java 中基于正则表达式/有效的解决方案来实现相同的目标。

WITH dataTab0 AS (SELECT TO_CHAR(to_date(tab_0_0.times),'YYYYMMDD')  AS TIME_ID_CATEGORY,tab_0_0.request_id AS PAGE_IMPRESSION  FROM full_contents_2

最佳答案

这样的事情怎么样?本质上是将 TIME_ID_CATEGORY 或 PAGE_IMPRESSION 匹配到第 1 组,并将后面的任何内容(即后缀)作为第 2 组。

(TIME_ID_CATEGORY|PAGE_IMPRESSION)(\w+)

Regex Demo

然后只需将第 2 组的内容替换为空字符串即可。或者只替换为第 1 组,这也会去掉后缀(见下面的代码片段)。

示例代码片段:

public static void main(String args[]) throws Exception {

String line = "WITH dataTab0 AS (SELECT TO_CHAR(to_date(tab_0_0.times),'YYYYMMDD') AS TIME_ID_CATEGORYe93bc60a0041,tab_0_0.request_id AS PAGE_IMPRESSIONf6beefc154b44e4b FROM full_contents_2";
Pattern p = Pattern.compile("(TIME_ID_CATEGORY|PAGE_IMPRESSION)(\\w+)");
Matcher m = p.matcher(line);
if (m.find()) {
String output = m.replaceAll("$1");
System.out.println(output);
//WITH dataTab0 AS (SELECT TO_CHAR(to_date(tab_0_0.times),'YYYYMMDD') AS TIME_ID_CATEGORY,tab_0_0.request_id AS PAGE_IMPRESSION FROM full_contents_2

}

}

关于java - 正则表达式:在段落中出现任何字符串列表后删除任何单词中的后缀字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57696493/

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