• 小结:5分钟学会正则,解决98%爬虫需要做字符串提取的工作


    介绍几种非常常用的特殊字符,解决98%爬虫需要做字符串提取的工作。

    正则表达式最常见的字符

    • 1)特殊字符:就是一些有特殊含义的字符。 $ () * + . [ ? ^ { |

    • 2)限定符:用来指定正则表达式的一个给定组件必须要出现多少次才能满足匹配。* + ? {n} {n,} {n,m}

    • 3)定位符:用来描述字符串或单词的边界。^ $

    • 4)其他字符:w W s S d

    直接进入python示例

    1、介绍^ . * $的用法

    • 1)^ 匹配输入字符串开始的位置。
    • 2)字符. 匹配除换行符 之外的任何单字符。
    • 3)字符* 匹配前面的子表达式零次或多次。
    • 4)字符 $ 匹配输入字符串的结尾位置。
    # -*- coding:utf-8 -*-
    
    '''
    by yizhiamumu
    20171101
    '''
    
    import  re
    
    
    '''1介绍^ . * $的用法
    ^ 匹配输入字符串开始的位置。
    . 匹配除换行符 
     之外的任何单字符。
    * 匹配前面的子表达式零次或多次。
    $ 匹配输入字符串的结尾位置。
    '''
    
    #需要匹配的字符串
    line = "hello world"
    
    #1.打印 以h 开头的文字
    
    regex_str = '^h'
    if re.match(regex_str, line):
        print "1, 打印以h 开头的文字"
    
    #2.打印 以h 开头,后面跟着一个字符串
    regex_str = '^h.'
    if re.match(regex_str, line):
        print  "2, 打印以h 开头,后面跟着一个字符串"
    
    #3.打印以h 开头,跟任意数量字符串
    regex_str = '^h*'
    if re.match(regex_str, line):
        print  "3打印以h 开头,后面任意数量的字符串"
    
    #4以d 结尾
    regex_str = '.*d$'
    if re.match(regex_str, line):
        print "4 匹配以d 结尾的字符串"
    
    #5 以h 开头,以d 结尾,中间只有任意一个字符串
    regex_str = '^h.d$'
    if re.match(regex_str, line):
        print  "5 以h 开头, 以d 结尾,中间只有任意一个字符串"
    
    #6 以h 开头, 以d 结尾,中间任意字符串
    regex_str = '^h.*d$'
    if re.match(regex_str, line):
        print "6 h开头,以d 结尾,中间任意一个字符串"

    打印日志:

    image

    2、介绍() ?用法

    • ()标记一个子表达式的开始和结束位置。
    • ?匹配前面的子表达式零次或一次,或指明一个非贪婪限定符。
    '''2介绍() ?用法
    ()标记一个子表达式的开始和结束位置。
    ?匹配前面的子表达式零次或一次,或指明一个非贪婪限定符。
    '''
    
    line = "hellohailhelloworld"
    
    #获取hail
    
    regex_str = '.*(h.*h).*'
    match_obj = re.match(regex_str, line)
    if match_obj:
        print "获取hail,打印结果为hailh"
        print match_obj.group(1)
    
    #使用非贪婪限定符,强制从左开始匹配
    regex_str = '.*?(h.*h).*'
    match_obj = re.match(regex_str, line)
    if match_obj:
        print "使用非贪婪限定符,强制从左开始匹配"
        print match_obj.group(1)
    
    #使用非贪婪限定符,强制从左开始匹配,遇到第一个h 就停止匹配
    regex_str = '.*?(h.*?h).*'
    match_obj = re.match(regex_str, line)
    if match_obj:
        print "使用非贪婪限定符,强制从左开始匹配,遇到第一个h 就停止匹配"
        print  match_obj.group(1)

    打印日志:

    image

    3、介绍+ {n} {n,} {n,m}用法

    • 1)+匹配前面的子表达式一次或多次。

    例如,'zo+' 能匹配 "zo" 以及 "zoo",但不能匹配 "z"。+ 等价于 {1,}。

    • 2){n} n 是一个非负整数。匹配确定的 n 次。

    例如,'o{2}' 不能匹配 "Bob" 中的 'o',但是能匹配 "food" 中的两个 o。

    • 3){n,} n 是一个非负整数。至少匹配n 次。

    例如,'o{2,}' 不能匹配 "Bob" 中的 'o',但能匹配 "foooood" 中的所有 o。'o{1,}' 等价于 'o+'。'o{0,}' 则等价于 'o*'。

    • 4){n,m} m 和 n 均为非负整数,其中n <= m。最少匹配 n 次且最多匹配 m 次。

    例如,"o{1,3}" 将匹配 "fooooood" 中的前三个 o。'o{0,1}' 等价于 'o?'。请注意在逗号和两个数之间不能有空格。

    '''
    3、介绍+ {n} {n,} {n,m}用法
    1)+匹配前面的子表达式一次或多次。
    例如,'zo+' 能匹配 "zo" 以及 "zoo",但不能匹配 "z"。+ 等价于 {1,}。
    2){n} n 是一个非负整数。匹配确定的 n 次。
    例如,'o{2}' 不能匹配 "Bob" 中的 'o',但是能匹配 "food" 中的两个 o。
    3){n,} n 是一个非负整数。至少匹配n 次。
    例如,'o{2,}' 不能匹配 "Bob" 中的 'o',但能匹配 "foooood" 中的所有 o。'o{1,}' 等价于 'o+'。'o{0,}' 则等价于 'o*'。
    4){n,m} m 和 n 均为非负整数,其中n <= m。最少匹配 n 次且最多匹配 m 次。
    例如,"o{1,3}" 将匹配 "fooooood" 中的前三个 o。'o{0,1}' 等价于 'o?'。请注意在逗号和两个数之间不能有空格。
    
    '''
    
    line = "hellohailhihello world"
    
    #获取h 和 h 之间包含特定数量字符的子字符串
    #使用+, h和h 之间至少要有一个字符
    
    regex_str = '.*(h.+h).*'
    match_obj = re.match(regex_str, line)
    if match_obj:
        print "使用+,获取h 和 h 之间包含特定数量字符的子字符串,h 和 h 之间至少有一个字符"
        print match_obj.group(1)
    
    #使用{1}, h和h 之间至少有一个字符
    
    regex_str = '.*(h.{1}h).*'
    match_obj = re.match(regex_str, line)
    if match_obj:
        print "使用{1}, h和h 之间至少有一个字符"
        print  match_obj.group(1)
    
    #使用{2,} ,h 和 h 之间至少有两个字符
    regex_str = '.*(h.{2,}h).*'
    match_obj = re.match(regex_str, line)
    if match_obj:
        print "使用{2,} ,h 和 h 之间至少有两个字符"
        print  match_obj.group(1)
    
    # 使用{3,5} , h 和 h  之间的字符限定在3-5个
    regex_str = '.*(h.{3,5}h).*'
    match_obj = re.match(regex_str, line)
    if match_obj:
        print "使用{3,5} , h 和 h  之间的字符限定在3-5个"
        print  match_obj.group(1)

    打印日志:

    image

    4、介绍|[123] [0-9] [^1]用法

    • 1)|指明两项之间的一个选择。
    • 2)[123] 只要是123中的其中一个即可。
    • 3)[0-9] 只要是0-9中的任意数字即可。
    • 4)[^1] 非,只要不是1即可。
    '''
    4、介绍|[123] [0-9] [^1]用法
    
    1)|指明两项之间的一个选择。
    2)[123] 只要是123中的其中一个即可。
    3)[0-9] 只要是0-9中的任意数字即可。
    4)[^1] 非,只要不是1即可。
    
    '''
    line = "hello world"
    #   匹配hello world
    regex_str = '(hello world|hi world)'
    match_obj = re.match(regex_str, line)
    if match_obj:
        print "123"
        print match_obj.group(1)
    # 使用[] 匹配手机号, 1之后只要是3578 中的一个即可,后面0-9 中的数字出现9次
    line = '18662342234'
    regex_str = '1[3578][0-9]{9}'
    match_obj = re.match(regex_str, line)
    if match_obj:
        print "正确的手机号"
    
    
    # 使用[]匹配手机号, 1之后只要是3578 中的一个即可,后面数字不能为1
    regex_str = '1[3578][^1]{9}'
    match_obj = re.match(regex_str, line)
    if match_obj:
        print "使用[]匹配手机号, 1之后只要是3578 中的一个即可,后面数字不能为1"

    打印日志:

    image

    5、介绍s S w W d用法

    • 1)s匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [ f v]。
    • 2)S匹配任何非空白字符。等价于 [^ f v]。
    • 3)w 等价于[A-Za-z0-9_]。
    • 4)W 与w相反。
    • 5)d 所有数字,等价于[0-9]。
    '''
    5、介绍s S w W d用法
    
    1)s匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [ f
    
    	v]。
    2)S匹配任何非空白字符。等价于 [^ f
    
    	v]。
    3)w 等价于[A-Za-z0-9_]。
    4)W 与w相反。
    5)d 所有数字,等价于[0-9]。
    
    '''
    line = 'hello world'
    regex_str = '(hellosworld)'
    match_obj = re.match(regex_str, line)
    if match_obj:
        print "hello world 之间有空格"
    
    regex_str2 = '(helloSworld)'
    match_obj = re.match(regex_str2, line)
    if match_obj:
        print "不是空格"
    
    regex_str3 = '(hellowworld)'
    match_obj = re.match(regex_str3, line)
    if match_obj:
        print "hello world 之间有[A-Za-z0-9_]其中的字符"
    
    regex_str4 = '(helloWworld)'
    match_obj = re.match(regex_str4, line)
    if match_obj:
        print "hello world 之间不是[A-Za-z0-9_]其中的字符"
    
    line = 'hello world 007'
    regex_str5 = '.*?(d+)'
    match_obj = re.match(regex_str5, line)
    if match_obj:
        print "打印其中数字"
        print match_obj.group(1)

    打印日志:

    image

    over

  • 相关阅读:
    jquery左右滑动效果的实现
    解决IE6不支持position:fixed的bug
    简单的漂浮层
    CSS Image Sprite--网页图片应用处理方式
    浮动层-JS兼容IE6
    js搜索框输入提示(高效-ys8)
    fiddler抓包(移动端APP端)
    python系统介绍
    “多走的弯路”
    接口测试实例(jmeter)
  • 原文地址:https://www.cnblogs.com/yizhiamumu/p/9143784.html
Copyright © 2020-2023  润新知