字符串模式匹配问题

问题描述 投票:0回答:1

假设我们有一个长字符串,其中包含子字符串“cat”和“dog”以及其他随机字符,例如。

cat x dog cat x cat x dog x dog x cat x dog x cat

这里“x”代表任意随机字符序列(但不是“cat”或“dog”)。

我想要做的是找到每个“猫”,后面跟着除“狗”之外的任何字符,然后是“猫”。我想在每种情况下删除第一个“猫”实例。

在这种情况下,我想删除括号中的 [cat],因为在下一个“cat”之前没有“dog”:

cat x dog [cat] x cat x dog x dog x cat x dog x cat

最终结果是:

cat x dog x cat x dog x dog x cat x dog x cat

这怎么办?

我想到以某种方式使用正则表达式,如 VonC 推荐的 (n)(?=(n)) 这里

(cat)(?=(.*cat))

匹配字符串中所有的“cat”对。但我仍然不确定如何使用它来删除“猫”之前没有“狗”的每只猫。


我要解决的真正问题是 Java。但我真的只是在寻找通用的伪代码/正则表达式解决方案。

java regex string string-matching
1个回答
2
投票

您想通过一次 RE 调用来完成此操作是否有任何特殊原因?我不确定这在 RE 中是否真的可行。

如果我必须这样做,我可能会分两次进行。首先标记字符串中“cat”和“dog”的每个实例,然后编写一些代码来识别需要删除哪些猫,并在另一遍中执行此操作。

伪代码如下:

// Find all the cats and dogs
int[] catLocations = string.findIndex(/cat/);
int[] dogLocations = string.findIndex(/dog/);
int [] idsToRemove = doLogic(catLocations, dogLocations);

// Remove each identified cat, from the end to the front
for (int id : idsToRemove.reverse())
  string.removeSubstring(id, "cat".length());
© www.soinside.com 2019 - 2024. All rights reserved.