I need create a regular expression in ruby on rails for validate the user name following this rules:
1)- The user name accept only lowercase letters, numbers,dashes and underscores
2)- The first character must be a lowercase letter
3)- The username have as minimum 6 characters and 30 characters as maximun
I thought some like this, but I'm not very sure
/^ ([a-z][a-z\_-0-9]+){6,30} $/
I'd suggest the following:
/^[a-z][a-z\-0-9_]{5,29}$/
I'll break it down:
^[az]
- the sequence starts with ( ^
) a lowercase character [az\\-0-9_]
- any of the chars in that group {5,29}
- between 5 and 29 (30 max, minus the one at the start of the sequence) of the previous set of chars $
the end of the sequence You mentioned character classes - sure, you can use them. I typically don't because they're not as portable (someone may chime in and prove me wrong here) and I don't think they're as easy to understand, simply because I tend to forget which ones are which. YMMV
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.