[Python-12/100] and the regular expression string (Strings and regular expressions)

Copyright: Public Number: Fresh Site. QQ group: 690 274 ​​159. Reprinted with my blog, please attach a reprint address, thank you! ^ _ ^ I'm a hip-hop program ape freshman. https://blog.csdn.net/wuhongxia29/article/details/90713626

Reference: Python-100-Days

Day12 strings and regular expressions

Reference:
Regular Expressions 30 minutes introductory tutorial
online regular expression test
Kordsa
Pythton official documents - Regular

Using regular expressions

related information

Definition: tool string matching pattern.
Regular mind map
Regular Expressions

Special characters: escape, for example: decimal \, parentheses \ (and \).

Python support for canonical

re module
In Python re module core function

example

1. Verify that the user name and QQ

Validate the input user name and QQ number is valid and gives the corresponding prompt information
requirements: username must consist of letters, numbers or underscores and a length of between 6-20 characters, QQ number is 5-12 and the first number is not 0

import re


def match_qq_nickname():
    """
    验证输入用户名和QQ号是否有效并给出对应的提示信息

    要求:用户名必须由字母、数字或下划线构成且长度在6~20个字符之间,QQ号是5~12的数字且首位不能为0
    """
    # username = input(str('请输入用户名:'))
    username = 'freshman'
    # qq = input('请输入QQ:')
    qq = '1187206700'
    name_match = re.match(r'^[0-9a-zA-Z_]{6,20}$', username)
    # name_match = re.match(r'^\w{6, 20}$', username)
    qq_match = re.match(r'^[1-9]\d{4,11}$', qq)
    # qq_match = re.match(r'^\d{5, 12}$', qq)
    if not name_match:
        print('无效用户名')
    if not qq_match:
        print('无效qq')
    if name_match and qq_match:
        print('你输入的信息有效')


if __name__ == '__main__':
    match_qq_nickname()

# 结果
# 你输入的信息有效

2. Extract the domestic mobile phone numbers from some text.

The picture below is the end of 2017, the domestic three operators launched mobile phone number section.
Here Insert Picture Description

import re


def main():
    # 创建正则表达式对象,使用前瞻和回顾来保证手机号前后不应该出现数字
    
    pattern = re.compile(r'(?<=\D)1[34578]\d{9}(?=\D)')  
    sentence = '''重要的事情说8130123456789遍,我的手机号是13512346789这个靓号,
    不是15600998765,也是110或119,王大锤的手机号才是15600998765。'''
    # 查找所有匹配并保存到一个列表中
    mylist = re.findall(pattern, sentence)
    print(mylist)
    print('--华丽的分割线--')
    # 通过迭代器取出匹配对象并获得匹配的内容
    for temp in pattern.finditer(sentence):
        print(temp.group())
    print('--华丽分割线--')
    # 通过search函数指定搜索位置找出所有匹配
    m = pattern.search(sentence)
    while m:
        print(m.group())
        m = pattern.search(sentence, m.end())
        
main()

3. Replace the string of inappropriate content

import re


def main():
    sentence = '你丫是傻叉吗? 我操你大爷的. Fuck you.'
    purified = re.sub('[操丫]|fuck|shit', '*', sentence, flags=re.IGNORECASE)
    print(purified)
    
main()

4. Split string

import re


def main():
    poem = '窗前明月光,疑是地上霜。举头望明月,低头思故乡。'
    sentence_list = re.split(r'[,。,.]', poem)
    while '' in sentence_list:
        sentence_list.remove('')
    print(sentence_list)

main()

Guess you like

Origin blog.csdn.net/wuhongxia29/article/details/90713626