Regex - Does not contain certain Characters

折月煮酒 提交于 2019-11-26 08:48:56

问题


I need a regex to match if anywhere in a sentence there is NOT either < or >.

If either < or > are in the string then it must return false.

I had a partial success with this but only if my < > are at the beginning or end:

(?!<|>).*$

I am using .Net if that makes a difference.

Thanks for the help.


回答1:


^[^<>]+$

The caret in the character class ([^) means match anything but, so this means, beginning of string, then one or more of anything except < and >, then the end of the string.




回答2:


Here you go:

^[^<>]*$

This will test for string that has no < and no >

If you want to test for a string that may have < and >, but must also have something other you should use just

[^<>] (or ^.*[^<>].*$)

Where [<>] means any of < or > and [^<>] means any that is not of < or >.

And of course the mandatory link.



来源:https://stackoverflow.com/questions/4105956/regex-does-not-contain-certain-characters

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