猫史档案馆


正则表达式

用户:纯数孤傲纯数孤傲查看:0 回复:0 评论:0 创建时间:2019-11-02T14:54:42


上代码:

#! python3

if __name__ == '__main__'喵ile(r'\d{3}-\d{4}-\d{4}')
    match = regex.search("Dad's phone number is 喵.")
    match2 = regex.search("Mother's body is very good.")
    print(match.group(), match2)

    regex = re.compile(r'Batman|Tina Fey')
    match = regex.search("Batman and Tina Fey")
    match2 = regex.search("Tina Fey and Batman")
    match3 = regex.search("Bat woman and Tina")
    print(match.group(), match2.group(), match3)

    regex = re.compile(r'Bat( wo)?man')
    match = regex.search("The Adventures of Batman")
    match2 = regex.search("The Adventures of Bat woman")
    match3 = regex.search("The Adventures of Woman")
    print(match.group(), match2.group(), match3)

    regex = re.compile(r'Bat( wo)*man')
    match = regex.search("The Adventures of Bat wo woman")
    match2 = regex.search("The Adventures of Bat woman")
    match3 = regex.search("The Adventures of Batman")
    print(match.group(), match2.group(), match3.group())

    regex = re.compile(r'Bat( wo)+man')
    match = regex.search("The Adventures of Bat wo woman")
    match2 = regex.search("The Adventures of Bat wo wo woman")
    match3 = regex.search("The Adventures of Batman")
    print(match.group(), match2.group(), match3)

    regex = re.compile(r'(Ha){3,5}')
    regex2 = re.compile(r'(Ha){3,5}?')
    match = regex.search("HaHaHaHa")
    match2 = regex.search("HaHaHaHaHa")
    match21 = regex2.search("HaHaHaHaHa")
    match3 = regex.search("HaHa")
    print(match.group(), match2.group(), match21.group(), match3)

    regex = re.compile(r'\d\d\d-\d\d\d\d-\d\d\d\d')
    match = regex.search("Mom: 180-7312-9972, Dad: 喵")
    match2 = regex.findall("Mom: 180-7312-9972, Dad: 喵")
    print(match.group(), match2)

    regex = re.compile(r'[aeiouAEIOU]')
    regex2 = re.compile(r'[^aeiouAEIOU]')
    match = regex.findall("RoboCop eats baby food. BABY FOOD.")
    match2 = regex2.findall("RoboCop eats baby food. BABY FOOD.")
    print(match, match2)
    
    regex = re.compile(r'^Hello')
    regex2 = re.compile(r'\d$')
    regex3 = re.compile(r'^\d+$')
    match = regex.search("Hello World!")
    match2 = regex2.search("Your number is 42")
    match3 = regex3.search("1415926535")
    print(match.group(), match2.group(), match3.group())

    regex = re.compile(r'.at')
    match = regex.findall("The cat in the hat sat on the flat mat.")
    print(match)

    regex = re.compile(r'First Name: (.*) Last Name: (.*)')
    regex2 = re.compile(r'<.*>')
    regex21 = re.compile(r'<.*?>')
    match = regex.search("First Name: Al Last Name: Peter")
    match2 = regex2.search("<To serve man> for dinner.>")
    match3 = regex21.search("<To serve man> for dinner.>")
    print(match.group(), match2.group(), match3.group())

    regex = re.compile(r'.*')
    regex2 = re.compile(r'.*', re.DOTALL)
    match = regex.search("Serve the public trust.\nProtect the innocent.\nUphold the law.")
    match2 = regex2.search("Serve the public trust.\nProtect the innocent.\nUphold the law.")
    print(match.group(), match2.group())

    regex = re.compile(r'run', re.IGNORECASE)
    match = regex.search("He runs very fast.")
    match2 = regex.search("Running race")
    print(match.group(), match2.group())

    regex = re.compile(r'((\d{3}|\(\d{3}\))?(\s|-|\.)?\d{3}(\s|-|\.)\d{4}(\s*(ext|x|ext.)\s*\d{2,5})?)')
    regex2 = 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)
    regex3 = re.compile(r'foo', re.IGNORECASE | re.DOTALL)
    regex4 = re.compile(r'foo', re.IGNORECASE | re.DOTALL | re.VERBOSE)


回复

上一页1 页 / 共 0下一页