问题
Is there are way to do this?
So if X is the unwanted character, I'd be looking for something like (.*[^X])
The aim is to match every character in a string until an X turns up.
I've searched high and low, but can't find the answer.
回答1:
To match everything up to a certain character X, the simplest should be;
[^X]*
Simple refiddle to show it.
回答2:
Another flavour, which is close to what you had in mind originally:
(.*?)X
Greedy capture ? is the trick here. You will need to take the first captured group in code if you want to exclude X character from the result.
来源:https://stackoverflow.com/questions/16376284/how-do-i-match-any-character-except-x-in-a-regular-expression