A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© asd110 初级黑马   /  2018-11-15 08:21  /  1228 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

正则表达式
        按照一定规则的表达式
        记录文本规则的代码
        作用:
                从杂乱无章的字符串中提取符合规则的字符串
               
        re
                import re
                # re.match: 比较方法(从左往右开始比较匹配)
                result = re.match("\d", "3sd")
                # result.group()返回一个匹配结果对象
                result.group()
               
               
        # 单字符匹配
        """        .        匹配任意1个字符(除了\n)
                [ ]        匹配[ ]中列举的字符
                \d        匹配数字,即0-9
                \D        匹配非数字,即不是数字
                \s        匹配空白,即 空格,tab键(\t)
                \S        匹配非空白
                \w        匹配非特殊字符,即a-z、A-Z、0-9、_、汉字
                \W        匹配特殊字符,即非字母、非数字、非汉字        """
       
        #        For example:
        import re

        if __name__ == '__main__':
                #.            匹配任意1个字符(除了\n)
                # match(正则表达式, 杂乱无章的字符串)
                result = re.match("t.o", "t&o")
                if result:
                        print("匹配成功: ", result.group())
                else:
                        print("匹配失败")

                # [ ]        匹配[ ]中列举的字符
                result = re.match("t[abc]o", "tao")
                # result = re.match("t[abc]o", "too")
                if result:
                        print("匹配成功: ", result.group())
                else:
                        print("匹配失败")

                result = re.match("t[0-9]o", "t9o")
                # result = re.match("t\do", "t90o")
                if result:
                        print("匹配成功: ", result.group())
                else:
                        print("匹配失败")


                # \d        匹配数字,即0-9
                result = re.match("t\do", "t0o")
                # result = re.match("t\do", "t90o")
                if result:
                        print("匹配成功: ", result.group())
                else:
                        print("匹配失败")

                # \D        匹配非数字,即不是数字
                result = re.match("t\Do", "t&o")
                if result:
                        print("匹配成功: ", result.group())
                else:
                        print("匹配失败")

                # \s        匹配空白,即 空格,tab键
                result = re.match("t\so", "t o")
                result = re.match("t\so", "t\to")
                # result = re.match("t\so", "tao")
                if result:
                        print("匹配成功: ", result.group())
                else:
                        print("匹配失败")


                # \S        匹配非空白
                # result = re.match("t\So", "t o")
                # result = re.match("t\So", "t\to")
                result = re.match("t\So", "tao")
                if result:
                        print("匹配成功: ", result.group())
                else:
                        print("匹配失败")

                # \w        匹配非特殊字符,即a-z、A-Z、0-9、_、汉字
                result = re.match("\w", "孩")
                result = re.match("\w", "ABCD")
                # result = re.match("\w", "&")
                if result:
                        print("匹配成功: ", result.group())
                else:
                        print("匹配失败")

                # \W        匹配特殊字符,即非字母、非数字、非汉字
                # result = re.match("\W", "孩")
                # result = re.match("\W", "ABCD")
                result = re.match("\W", "&")
                if result:
                        print("匹配成功: ", result.group())
                else:
                        print("匹配失败")

                #匹配密码中的其中一位,密码是由字母、数字、下划线组成,请列举的方式匹配?
                result = re.match("[a-zA-Z0-9_]", "a")
                if result:
                        print("匹配成功: ", result.group())
                else:
                        print("匹配失败")
               
               
        # 多字符匹配
        """        *        匹配前一个字符出现0次或者无限次,即可有可无
                +        匹配前一个字符出现1次或者无限次,即至少有1次
                ?        匹配前一个字符出现1次或者0次,即要么有1次,要么没有
                {m}        匹配前一个字符出现m次
                {m,}        匹配前一个字符至少出现m次
                {,n}        匹配前一个字符最多出现n次
                {m,n}        匹配前一个字符出现从m到n次        """
               
        #        For example:
        import re

        if __name__ == '__main__':

                result = re.match("t.*o", "tljo")
                if result:
                        print("匹配结果: ", result.group())
                else:
                        print("匹配失败")

                # *        匹配前一个字符出现0次或者无限次,即可有可无
                # 需求:匹配出一个字符串第一个 字母为大小 字符,后面都是小写字母并且这些小写字母可
                # 有可无
                result = re.match("[a-zA-Z][a-z]*", "Tbcjo")
                result = re.match("[a-zA-Z][a-z]*", "TBC")
                # result = re.match("[a-zA-Z][a-z]*", "9TBC")

                if result:
                        print("匹配结果: ", result.group())
                else:
                        print("匹配失败")

                # +        匹配前一个字符出现1次或者无限次,即至少有1次
                # 需求:匹配一个字符串,第一个字符是t, 最后一个字符串是o, 中间至少有一个字符
                result = re.match("t.+o", "t9TBCo")
                # result = re.match("t.+o", "to")
                if result:
                        print("匹配结果: ", result.group())
                else:
                        print("匹配失败")

                # ?        匹配前一个字符出现1次或者0次,即要么有1次,要么没有
                # 需求:匹配出这样的数据,但是https 这个s可能有,也可能是http 这个s没有
                result = re.match("https?", "http")
                result = re.match("https?", "https")
                if result:
                        print("匹配结果: ", result.group())
                else:
                        print("匹配失败")

                # {m,n}        匹配前一个字符出现从m到n次
                #需求:匹配出,8到20位的密码,可以是大小写英文字母、数字、下划线
                result = re.match("[a-zA-Z0-9_]{8,20}", "http1234")
                result = re.match("[a-zA-Z0-9_]{8,20}", "httpsdfasf1234")
                # result = re.match("[a-zA-Z0-9_]{8,20}", "http")
                if result:
                        print("匹配结果: ", result.group())
                else:
                        print("匹配失败")

                # {m,}        匹配前一个字符出现至少m次
                result = re.match("[a-zA-Z0-9_]{8,}", "http123lsjdflajsdflaslfjlasjflasjflasf")
                if result:
                        print("匹配结果: ", result.group())
                else:
                        print("匹配失败")

                # {m}        匹配前一个字符出现m次
                result = re.match("[a-zA-Z0-9_]{8}", "http123lsjdflajsdflaslfjlasjflasjflasf")
                if result:
                        print("匹配结果: ", result.group())
                else:
                        print("匹配失败")

                # {,n}        匹配前一个字符出现最多n次
                result = re.match("[a-zA-Z0-9_]{,8}", "jsdfljafa")
                if result:
                        print("匹配结果: ", result.group())
                else:
                        print("匹配失败")
               
               
        # 匹配开头和结尾的正则表达式
        """        ^        匹配字符串开头
                $        匹配字符串结尾        """
        # 除了指定字符以外都匹配
        """        [^指定字符]        匹配非括号中的字符串        """
       
        #        For example:
        import re

        if __name__ == '__main__':

                result = re.match("^a", "aljsfl")
                if result:
                        print("匹配成功: ", result.group())
                else:
                        print("匹配失败")

                result = re.match("^a.*a$", "aljsfla")
                if result:
                        print("匹配成功: ", result.group())
                else:
                        print("匹配失败")

                # result = re.match("^a[4567]*a$", "a4545456767a")
                result = re.match("^a[^4567]*a$", "a123890abcdea")
                if result:
                        print("匹配成功: ", result.group())
                else:
                        print("匹配失败")

               
        # 匹配分组相关正则表达式
        """        |        匹配左右任意一个表达式
                (ab)        将括号中字符作为一个分组
                        group(0) 全部分组
                        group(1) 第一个分组
                \num        引用分组num匹配到的字符串
                        re.match("<([a-zA-Z1-6]{2-8})>.*</\\1>")
                        \\1代表应用第一个分组匹配到的字符串(第一个\是转义符)
                (?P<name>)        分组起别名
                (?P=name)        引用别名为name分组匹配到的字符串        """
       
       
        #        For example:
        import re

        if __name__ == '__main__':

                # | 匹配左右任意一个表达式
                # 需求:在列表中["apple", "banana", "orange", "pear"],匹配apple和pear
                l = ["apple", "banana", "orange", "pear"]

                for i in l:
                        result = re.match("apple|pear", i)
                        if result:
                                print("这是我想要的水果: ", result.group())
                        else:
                                print("这个不是我需要的水果:", i)

                # result = re.match("[a-zA-Z0-9_]{4,20}@163|qq|126\.com", "hello@163.com")
                # result = re.match("[a-zA-Z0-9_]{4,20}@163|qq|126\.com", "qq")
                result = re.match("[a-zA-Z0-9_]{4,20}@(163|qq|126)\.com", "hello@163.com")
                if result:
                        print("匹配成功: ", result.group())
                        # group默认返回的是第0组结果;
                        print("group(0): ", result.group(0))
                        # group(1): 指的是第一个()中匹配的内容;
                        print("group(1): ", result.group(1))
                else:
                        print("匹配失败:")

                # (ab) 将括号中字符作为一个分组
                result = re.match("([a-zA-Z0-9_]{4,20})@(163|qq|126)\.com", "hello@163.com")
                if result:
                        print("匹配成功: ", result.group())
                        # group默认返回的是第0组结果;
                        print("group(0): ", result.group(0))
                        # group(1): 指的是第一个()中匹配的内容;
                        print("group(1): ", result.group(1))
                        # group(2): 指的是第二个()中匹配的内容;
                        print("group(2): ", result.group(2))
                else:
                        print("匹配失败:")

                # 需求: 匹配qq:10567这样的数据,提取出来qq文字和qq号码

                result = re.match("(\w*qq):([1-9]\d{4,9})", "张三的qq:10567")
                if result:
                        print("1: ", result.group(1))
                        print("2: ", result.group(2))
                else:
                        print("匹配失败")

                # \num引用分组num匹配到的字符串

                # 需求:匹配出 <html> hh </html>
                #   <h1>标题</h1>
                #   <img>...</img>
                # result = re.match("<[a-zA-Z1-6]{2,8}>.+</[a-zA-Z1-6]{2,8}>", "<html> hh </html>")
                # result = re.match("<([a-zA-Z1-6]{2,8})>.+</\\1>", "<html> hh </h1>")
                result = re.match("<([a-zA-Z1-6]{2,8})>.+</\\1>", "<html> hh </html>")
                if result:
                        print("匹配成功: ", result.group())
                else:
                        print("匹配失败")

                # 需求:匹配出 < html > < h1 > www.itcast.cn < / h1 > < / html >
                result = re.match("<([a-zA-Z1-6]{2,8})><([a-zA-Z1-6]{2,8})>.+</\\2></\\1>", "<html><h1> hh </h1></html>")
                if result:
                        print("匹配成功: ", result.group())
                else:
                        print("匹配失败")


                # (?P < name >)    分组起别名
                # (?P = name)    引用别名为name分组匹配到的字符串

                # 需求:匹配出 < html > < h1 > www.itcast.cn < / h1 > < / html >
                result = re.match("<(?P<t1>[a-zA-Z1-6]{2,8})><(?P<t2>[a-zA-Z1-6]{2,8})>.+</(?P=t2)></(?P=t1)>", "<html><h1> hh </h1></html>")
                if result:
                        print("匹配成功: ", result.group())
                else:
                        print("匹配失败")
               
               
               
               
               
               
               
               
               
               
               
               

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马