Python:使用re.match检查字符串

时间:2021-05-03 14:32:33

I need to write func to check str. If should fit next conditions:

我需要编写func来检查str。如果适合下一个条件:

1) str should starts with alphabet - ^[a-zA-Z]

1)str应以字母开头 - ^ [a-zA-Z]

2) str may contains alphabet, numbers, one . and one -

2)str可能包含字母,数字,一个。一个 -

3) str should ends with alphabet or number

3)str应以字母或数字结尾

4) length of str should be from 1 to 50

4)str的长度应为1至50

def check_login(str):
    flag = False
    if match(r'^[a-zA-Z][a-zA-Z0-9.-]{1,50}[a-zA-Z0-9]$', str):
        flag = True
    return flag

But it should mean that it starts with alphabet, length of [a-zA-Z0-9.-] is more than 0 and less 51 and it ends with [a-zA-Z0-9]. How can I limit quantity of . and - and write length's limit to all expression?

但它应该意味着它以字母开头,[a-zA-Z0-9.-]的长度大于0且小于51,它以[a-zA-Z0-9]结束。我怎样才能限制数量。并且 - 并且对所有表达式写长度限制?

I mean that a - should returns true, qwe123 also true.

我的意思是 - 应该返回true,qwe123也是如此。

How can I fix that?

我该如何解决这个问题?

1 个解决方案

#1


2  

You will need lookaheads:

你需要前瞻:

^                              # start of string
    (?=^[^.]*\.?[^.]*$)        # not a dot, 0+ times, a dot eventually, not a dot
    (?=^[^-]*-?[^-]*$)         # same with dash
    (?=.*[A-Za-z0-9]$)         # [A-Za-z0-9] in the end
    [A-Za-z][-.A-Za-z0-9]{,49} 
$

See a demo on regex101.com.

请参阅regex101.com上的演示。


Which in Python might be:

import re

rx = re.compile(r'''
^                        # start of string
    (?=^[^.]*\.?[^.]*$)  # not a dot, 0+ times, a dot eventually, not a dot
    (?=^[^-]*-?[^-]*$)   # same with dash
    (?=.*[A-Za-z0-9]$)   # [A-Za-z0-9] in the end
    [A-Za-z][-.A-Za-z0-9]{,49} 
$
''', re.VERBOSE)

strings = ['qwe123', 'qwe-123', 'qwe.123', 'qwe-.-123', '123-']

def check_login(string):
    if rx.search(string):
        return True
    return False

for string in strings:
    print("String: {}, Result: {}".format(string, check_login(string)))

This yields:

String: qwe123, Result: True
String: qwe-123, Result: True
String: qwe.123, Result: True
String: qwe-.-123, Result: False
String: 123-, Result: False

#1


2  

You will need lookaheads:

你需要前瞻:

^                              # start of string
    (?=^[^.]*\.?[^.]*$)        # not a dot, 0+ times, a dot eventually, not a dot
    (?=^[^-]*-?[^-]*$)         # same with dash
    (?=.*[A-Za-z0-9]$)         # [A-Za-z0-9] in the end
    [A-Za-z][-.A-Za-z0-9]{,49} 
$

See a demo on regex101.com.

请参阅regex101.com上的演示。


Which in Python might be:

import re

rx = re.compile(r'''
^                        # start of string
    (?=^[^.]*\.?[^.]*$)  # not a dot, 0+ times, a dot eventually, not a dot
    (?=^[^-]*-?[^-]*$)   # same with dash
    (?=.*[A-Za-z0-9]$)   # [A-Za-z0-9] in the end
    [A-Za-z][-.A-Za-z0-9]{,49} 
$
''', re.VERBOSE)

strings = ['qwe123', 'qwe-123', 'qwe.123', 'qwe-.-123', '123-']

def check_login(string):
    if rx.search(string):
        return True
    return False

for string in strings:
    print("String: {}, Result: {}".format(string, check_login(string)))

This yields:

String: qwe123, Result: True
String: qwe-123, Result: True
String: qwe.123, Result: True
String: qwe-.-123, Result: False
String: 123-, Result: False