How do I match [Any Character Except X] in a regular expression?

夙愿已清 提交于 2019-12-12 20:26:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!