Imagine we have a long string containing the substrings "cat" and "dog", as well as other random characters, for example.
cat x dog cat x cat x dog x dog x cat x dog x cat
Here, 'x' represents any random sequence of characters (but not "cat" or "dog").
What I want to do is find each βcatβ, followed by any characters except βdogβ and then βcatβ. I want to delete this first instance of "cat" in each case.
In this case, I would like to remove the bracket [cat], because after it there will be no βdogβ before the next βcatβ:
cat x dog [cat] x cat x dog x dog x cat x dog x cat
Eventually:
cat x dog x cat x dog x dog x cat x dog x cat
How can I do that?
I was thinking of somehow using a regex like (n) (? = (N)) as recommended by VonC here
(cat)(?=(.*cat))
to match all the "cat" pairs in a string. But I still don't know how I can use this to remove every cat that is not followed by a βdogβ in front of a βcatβ.
The real problem I'm solving is Java. But I'm really just looking for a general pseudo-code / regular expression solution.
string regex pattern-matching
nodmonkey
source share