正则表达式——以字母开头和结尾,只接受字母、数字和_ [英] Regular expression - starting and ending with a letter, accepting only letters, numbers and _

查看:104
本文介绍了正则表达式——以字母开头和结尾,只接受字母、数字和_的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个正则表达式,指定文本应以字母开头,每个字符应为字母、数字或下划线,一行中不应有 2 个下划线,并且应以字母或数字.目前,我唯一拥有的是 ^[a-zA-Z]\w[a-zA-Z1-9_] 但这似乎不能正常工作,因为它只匹配3 个字符,并允许重复下划线.我也不知道如何指定最后一个字符的要求.

I'm trying to write a regular expression which specifies that text should start with a letter, every character should be a letter, number or underscore, there should not be 2 underscores in a row and it should end with a letter or number. At the moment, the only thing I have is ^[a-zA-Z]\w[a-zA-Z1-9_] but this doesn't seem to work properly since it only ever matches 3 characters, and allows repeated underscores. I also don't know how to specify requirements for the last character.

推荐答案

^[A-Za-z][A-Za-z0-9]*(?:_[A-Za-z0-9]+)*$

这篇关于正则表达式——以字母开头和结尾,只接受字母、数字和_的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆