Reputation: 6876
I would like to have a regular expression that checks if a string contains only upper and lowercase letters, numbers, and underscores, but underscore should not be a first or last character in a string for that I am trying with below in Python.
"^[a-zA-Z0-9_]*$"
Upvotes: 3
Views: 2683
Reputation: 189689
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.
Upvotes: 1
Reputation: 56819
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 _
.
Upvotes: 4