vachee
vachee

Reputation: 445

Valid single character with dot at regex end

I need to realize the following regex:

[a-zA-Z0-9_]+.

Now I can't figure out why "1" or "a" is not valid.

In addition, the following examples:

 abc_123_1 
 _____abc___301 
 1 
 a 

Should be valid, too.

Thank you for your help.

Upvotes: 0

Views: 287

Answers (2)

Sven
Sven

Reputation: 22693

Note: based on the title of the question, I'm assuming that the expression you are using is [a-zA-Z0-9_]+. (at the time of writing, the question has been edited by someone other than the OP to remove that dot).

Your regex currently requires "at least one of a-z, A-Z, 0-9 or _", followed by "exactly one of any character". Therefore, it requires at least two characters to match.

The string "1" and "a" have only one character, and therefore do not match. Given your valid examples, are you sure you need that dot at the end?

The expression [a-zA-Z0-9_]+ (without the dot) would match all your examples, including single character ones.

Upvotes: 3

Andras Sebo
Andras Sebo

Reputation: 1140

Just lose the dot (.) at the end of the expression and it's gonna be working, even with "1" or "a".

[a-zA-Z0-9_]+

Upvotes: 0

Related Questions