zoukankan      html  css  js  c++  java
  • python中几个常见正则例子:

    匹配手机号:

    1 phone_str = "hey my name is alex, and my phone number is 13651054607, please call me if you are pretty!"
    2 phone_str2 = "hey my name is alex, and my phone number is 18651054604, please call me if you are pretty!"
    3  
    4 m = re.search("(1)([358]d{9})",phone_str2)
    5 if m:
    6     print(m.group())
    View Code

    匹配IP V4:

    1 ip_addr = "inet 192.168.60.223 netmask 0xffffff00 broadcast 192.168.60.255"
    2  
    3 m = re.search("d{1,3}.d{1,3}.d{1,3}.d{1,3}", ip_addr)
    4  
    5 print(m.group())
    View Code

    分组匹配地址:

    contactInfo = 'Oldboy School, Beijing Changping Shahe: 010-8343245'
    match = re.search(r'(w+), (w+): (S+)', contactInfo) #分组
    
    match = re.search(r'(?P<last>w+), (?P<first>w+): (?P<phone>S+)', contactInfo)
    View Code

    匹配email:

    email = "alex.li@126.com   http://www.oldboyedu.com"
     
    m = re.search(r"[0-9.a-z]{0,26}@[0-9.a-z]{0,20}.[0-9a-z]{0,8}", email)
    print(m.group())
    View Code
    contactInfo = 'Oldboy School, Beijing Changping Shahe: 010-8343245'
    match = re.search(r'(w+), (w+): (S+)', contactInfo) #分组
    """
    >>> match.group(1)
      'Doe'
      >>> match.group(2)
      'John'
      >>> match.group(3)
      '555-1212'
    """
    match = re.search(r'(?P<last>w+), (?P<first>w+): (?P<phone>S+)', contactInfo)
  • 相关阅读:
    分小组
    抽签
    快速排序
    Zookeeper一致性协议原理Zab
    Zookeeper架构、ZAB协议、选举
    spring AOP
    spring bean生命周期
    [LeetCode] 35. Search Insert Position ☆(丢失的数字)
    [LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆
    代理模式和装饰器模式的区别
  • 原文地址:https://www.cnblogs.com/zijue/p/9809075.html
Copyright © 2011-2022 走看看