我想使用正则表达式验证包含以下内容的字符串:
- 必须仅包含小写字母
- 第一个字符必须是字母。
- 其余字符必须与正则表达式
[a-z0-9_.]
匹配
我使用/^[\w\s\-.]*$/
来验证字符串。但它允许像(._)这样的第一个字符
为什么不坚持你的要求呢?
[a-z]
[a-z0-9_.]
-> 你的正则表达式:/^[a-z][a-z0-9_.]*$/
input =~ /^[a-z][a-z0-9_.]*$/
试试这个
data = "helloworld_12_."
data =~/^[a-z][a-z\d._]*$/
puts $&
这个应该适用于你。
/^[a-z][a-z\d._]*/
/ - Start of Regex
^ - The string will start with a lowercase letter
[a-z\d._]* - The string will have zero or more lowercase letters, digits, etc.
/ - End of Regex
"ab12._c%%%"[/^[a-z][a-z\d._]*/] => "ab12._c"
。 - Cary Swoveland