简体   繁体   中英

2 simple Regular Expressions

I am a newbie with regular expressions. I am trying to make 2 of them for my website:

The 1st one: Any characters can be used, but if there is a < and/or a > any where in the string, this is not allowed.

The 2nd one: The same as the above, except a zero-length or blank string is also not allowed.

All I have so far is maybe [^<>] for the 1st one, and maybe ^\\S+$ in the 2nd one, but I don't think they even meet my requirements properly. Help appreciated.

( Note : I will be using these in an ASP.NET RegularExpressionValidator control)

Any characters can be used, but if there is a < and/or a > any where in the string, this is not allowed.

^[^<>]*$

See this demo . [^...] is a negated character class that matches any character but the ones defined in it. * matches 0+ characters (thus, an empty match is allowed). ^ asserts the position at the beginning of the string and $ - at the end.

The same as the above, except a zero-length or blank string is also not allowed.

^(?!\s+$)[^<>]+$

See another demo

The (?!\\s+$) negative lookahead ( \\s+ matches 1+ whitespaces and $ asserts the position at the end of the string) here prevents matching a string that is whitespace only. The + quantifier matches 1+ characters (so, empty matches are not allowed).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM