Regular expression - starting and ending with a letter, accepting only letters, numbers and _

后端 未结 4 1144
名媛妹妹
名媛妹妹 2020-12-14 17:12

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

4条回答
  •  感情败类
    2020-12-14 17:55

    Here's a solution using a negative lookahead (not supported in all regex engines):

    ^[a-zA-Z](((?!__)[a-zA-Z0-9_])*[a-zA-Z0-9])?$
    

    Test that it works as expected:

    import re
    tests = [
       ('a', True),
       ('_', False),
       ('zz', True),
       ('a0', True),
       ('A_', False),
       ('a0_b', True),
       ('a__b', False),
       ('a_1_c', True),
    ]
    
    regex = '^[a-zA-Z](((?!__)[a-zA-Z0-9_])*[a-zA-Z0-9])?$'
    for test in tests:
       is_match = re.match(regex, test[0]) is not None
       if is_match != test[1]:
           print "fail: "  + test[0]
    

提交回复
热议问题