python 检测电话号码代码 练习记录

def isPhoneNumber(text):
    lens = len(text)
    texts = text.replace('-', '')
    if lens != 12:
        if lens < 12:
            return '电话号码长度不足,你输入了{0}个字符!'.format(lens)
        else:
            return '电话号码过长,你输入了{0}个字符!'.format(lens)
    elif text[3] != '-':
        return '第三个数字后必须跟符号-'
    elif text[7] !='-':
        return '第六个数字后必须跟符号-'
    elif not texts.isdecimal():
        return '电话号码的格式错误(000-000-0000)'
    else:
        return '电话号码格式正确!'
print(isPhoneNumber('444-000-0000'))

字符串中间插件具有意义的电话号码代码如下

def isPhoneNumber(text):
    lens = len(text)
    texts = text.replace('-', '')
    if lens != 12:
        # if lens < 12:
            # return '电话号码长度不足,你输入了{0}个字符!'.format(lens)
        # else:
            # return '电话号码过长,你输入了{0}个字符!'.format(lens)
         return False
    elif text[3] != '-' or text[7] !='-':
        return False
    # elif text[7] != '-':
    #     return '第六个数字后必须跟符号-'
    elif not texts.isdecimal():
        return False
    else:
        return True

# print(isPhoneNumber('444-000-0000'))
message = 'Call me at 415-555-1011 tomorrow. 415-555-9999 is my office.'
for i in range(len(message)):
    chunk = message[i:i + 12]
    if isPhoneNumber(chunk):
        print('Phone number found:' + chunk)

python 正则表达式匹配 

 
 
import re

message = '''
Call me at 415-555-1011 tomorrow.
'''
phoneNumRegex=re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
mo =phoneNumRegex.search(message)
print(mo.group())
匹配所有负责规则的电话号码
import re

phoneNumRegex=re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
mo=phoneNumRegex.findall('My number is 300-455-4999 or 466-999-4564')
print(mo)
    def findall(self, string, pos=0, endpos=-1):
        """Return a list of all non-overlapping matches of pattern in string.

        :type string: T <= bytes | unicode
        :type pos: int
        :type endpos: int
        :rtype: list[T]
        """
        pass

\d

Any numeric digit from 0 to 9.

\D

Any character that is not a numeric digit from 0 to 9.

\w

Any letter, numeric digit, or the underscore character. (Think of this as matching “word” characters.)

\W

Any character that is not a letter, numeric digit, or the underscore character.

\s

Any space, tab, or newline character. (Think of this as matching “space” characters.)

\S

Any character that is not a space, tab, or newline.
This chapter covered a lot of notation, so here’s a quick review of what you learned:

The ? matches zero or one of the preceding group.

The * matches zero or more of the preceding group.

The + matches one or more of the preceding group.

The {n} matches exactly n of the preceding group.

The {n,} matches n or more of the preceding group.

The {,m} matches 0 to m of the preceding group.

The {n,m} matches at least n and at most m of the preceding group.

{n,m}? or *? or +? performs a nongreedy match of the preceding group.

^spam means the string must begin with spam.

spam$ means the string must end with spam.

The . matches any character, except newline characters.

\d, \w, and \s match a digit, word, or space character, respectively.

\D, \W, and \S match anything except a digit, word, or space character, respectively.

[abc] matches any character between the brackets (such as a, b, or c).

[^abc] matches any character that isn’t between the brackets.

正则匹配电话号码:

phoneRegex = re.compile(r'''(#这里是三个单引号,发布后会加两个单引号 删了即可
    (\d{3}|\(\d{3}\))?            # area code
    (\s|-|\.)?                    # separator
    \d{3}                         # first 3 digits
    (\s|-|\.)                     # separator
    \d{4}                         # last 4 digits
    (\s*(ext|x|\#|ext.)\s*\d{2,5})?  # extension
    )''', re.VERBOSE)
mo = phoneRegex.search('(400)-555-8000 # 434)').group()
print(mo)

猜你喜欢

转载自blog.csdn.net/tianpingxian/article/details/80294948