简体   繁体   中英

Regular expression for alphanumeric string, underscore should not be a first or last character

我想有一个正则表达式,检查字符串是否只包含大写和小写字母,数字和下划线,但下划线不应该是字符串中的第一个或最后一个字符,我在下面尝试使用Python。

"^[a-zA-Z0-9_]*$"   

You can use this regex:

^(?!_)\w*(?<!_)$

\\w is equivalent to [A-Za-z0-9_]

I use negative look ahead (?!) to check the first character is not _ , and negative look-behind (?<!) to check the last character is not _ .

Specify an optional end with your criteria.

'^([a-zA-Z0-9]([a-zA-Z0-9_]*[a-zA-Z0-9])?)?$'

The outer parentheses are to permit an empty string, like your original attempt. If you do not wish to match an empty string, you can simplify by removing the beginning parenthesis, and the closing parenthesis with a question mark quantifier.

Also note that I have used capturing parentheses for simplicity; converting the opening parentheses to non-capturing (?: will supposedly also make it go a little faster, although in this simple case, it can hardly matter.

As suggested by @JoelCornett, you can use re.match to explicitly search only at the start if the string and drop the ^ anchor.

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