zoukankan      html  css  js  c++  java
  • python_day5学习笔记

    一、正则表达式

    字符:

      d 匹配任何十进制数:相当于类[0-9]

      D 匹配任何非数字字符:相当于类[^0-9]

      s  匹配任何空白字符:相当于类[  fv]

      S  匹配任何非空白字符:相当于类[^  fv]

      w 匹配任何字母数字字符:相当于类[a-zA-Z0-9_]

      W 匹配任何非字母数字字符:相当于类[^a-zA-Z0-9_]

       匹配一个单词边界,也就是指单词和空格间的位置

      . 匹配除换行符以外的任意字符

      ^ 匹配字符串的开始

      $ 匹配字符串的结束

    次数:

      * 重复零次或更多次
      + 重复一次或更多次
      ? 重复零次或一次
      {n} 重复n次
      {n,} 重复n次或更多次
      {n,m} 重复n到m次

    • findall
    1 # findall,获取非重复的匹配列表;如果有一个组则以列表形式返回,且每一个匹配均是字符串;如果模型中有多个组,则以列表形式返回,且每一个匹配均是元祖;
    2 # 空的匹配也会包含在结果中
    3 #findall(pattern, string, flags=0)
     1 import  re
     2 # 无分组
     3 origin = "hello alex bcd abcd lge acd 19"
     4 r = re.findall("aw+",origin)
     5 print(r)
     6 
     7 # 有分组
     8 origin = "hello alex bcd abcd lge acd 19"
     9 r = re.findall("a((w*)c)(d)", origin)
    10 print(r)

    运行结果:

    1 ['alex', 'abcd', 'acd']
    2 [('bc', 'b', 'd'), ('c', '', 'd')]
    1 >>> re.findall('d','ww3wa8.d')
    2 ['3', '8']
    3 >>> re.findall('w','ww3wa8.d0')
    4 ['w', 'w', '3', 'w', 'a', '8', 'd', '0']
    5 >>> re.findall('s','ww3 wa8.d0')
    6 [' ']
    7 >>> re.findall('[d]','ww3 wa8.d0')
    8 ['3', '8', '0']
    • match
    1 # match,从起始位置开始匹配,匹配成功返回一个对象,未匹配成功返回None
    2  match(pattern, string, flags=0)
    3 # pattern: 正则模型
    4 # string : 要匹配的字符串
    5 # falgs  : 匹配模式
     origin = "hello alex bcd abcd lge acd 19"
    1
    # 无分组 2 r = re.match("hw+", origin) 3 print(r.group()) # 获取匹配到的所有结果 4 print(r.groups()) # 获取模型中匹配到的分组结果 5 print(r.groupdict()) # 获取模型中匹配到的分组结果 6 7 # 有分组 8 9 # 为何要有分组?提取匹配成功的指定内容(先匹配成功全部正则,再匹配成功的局部内容提取出来) 10 11 r = re.match("h(w+).*(?P<name>d)$", origin) 12 print(r.group()) # 获取匹配到的所有结果 13 print(r.groups()) # 获取模型中匹配到的分组结果 14 print(r.groupdict()) # 获取模型中匹配到的分组中所有执行了key的组

    运行结果:

    1 hello
    2 ()
    3 {}
    4 hello alex bcd abcd lge acd 19
    5 ('ello', '9')
    6 {'name': '9'}
    • search
    1 # search,浏览整个字符串去匹配第一个,未匹配成功返回None
    2 # search(pattern, string, flags=0)
     1 import  re
     2 origin = "hello alex bcd abcd lge acd 19"
     3 
     4 # 无分组
     5 r = re.search("aw+", origin)
     6 print(r.group())     # 获取匹配到的所有结果
     7 print(r.groups())    # 获取模型中匹配到的分组结果
     8 print(r.groupdict()) # 获取模型中匹配到的分组结果
     9 
    10 # 有分组
    11 r = re.search("a(w+).*(?P<name>d)$", origin)
    12 print(r.group())     # 获取匹配到的所有结果
    13 print(r.groups())    # 获取模型中匹配到的分组结果
    14 print(r.groupdict()) # 获取模型中匹配到的分组中所有执行了key的组
    15 
    16 运行结果:
    17 
    18 alex
    19 ()
    20 {}
    21 alex bcd abcd lge acd 19
    22 ('lex', '9')
    23 {'name': '9'}
    • sub
    1 # sub,替换匹配成功的指定位置字符串
    2  
    3 sub(pattern, repl, string, count=0, flags=0)
    4 # pattern: 正则模型
    5 # repl   : 要替换的字符串或可执行对象
    6 # string : 要匹配的字符串
    7 # count  : 指定匹配个数
    8 # flags  : 匹配模式
    1  # 与分组无关
    2  origin = "hello alex bcd alex lge alex acd 19"
    3  r = re.sub("aw+", "999", origin, 2)
    4  print(r)
    5 
    6 运行结果:
    7 hello 999 bcd 999 lge alex acd 19
    • split
    1 # split,根据正则匹配分割字符串
    2  
    3 split(pattern, string, maxsplit=0, flags=0)
    4 # pattern: 正则模型
    5 # string : 要匹配的字符串
    6 # maxsplit:指定分割个数
    7 # flags  : 匹配模式
     1  # 无分组
     2  origin = "hello alex bcd alex lge alex acd 19"
     3  r = re.split("alex", origin, 1)
     4  print(r)
     5 
     6 # 有分组    
     7 origin = "hello alex bcd alex lge alex acd 19"
     8 r1 = re.split("(alex)", origin, 1)
     9 print(r1)
    10 r2 = re.split("(al(ex))", origin, 1)
    11 print(r2)
    12 
    13 运行结果:
    14 ['hello ', ' bcd alex lge alex acd 19']
    15 ['hello ', 'alex', ' bcd alex lge alex acd 19']
    16 ['hello ', 'alex', 'ex', ' bcd alex lge alex acd 19']
    1 常用正则表达式:
    2 IP:
    3 ^(25[0-5]|2[0-4]d|[0-1]?d?d)(.(25[0-5]|2[0-4]d|[0-1]?d?d)){3}$
    4 手机号:
    5 ^1[3|4|5|8][0-9]d{8}$
    6 邮箱:
    7 [a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+

    二、random

    1 import random
    2 
    3 print(random.random())
    4 print(random.randint(1, 2))
    5 print(random.randrange(1, 10))
    6 print(random.randrange(2,10,2)) #从指定范围内,按指定基数递增的集合中 获取一个随机数

    随机验证码举例:

     1 import  random
     2 
     3 temp = ""
     4 for i in range(6):
     5     num = random.randrange(0,4)
     6     if num == 3 or num == 1:
     7         rad1 = random.randrange(0,10)
     8         temp = temp + str(rad1)
     9     else:
    10         rad2 = random.randrange(65,91)
    11         c = chr(rad2)
    12         temp =temp + c
    13 print(temp)
  • 相关阅读:
    HDU 2196 Computer
    HDU 1520 Anniversary party
    POJ 1217 FOUR QUARTERS
    POJ 2184 Cow Exhibition
    HDU 2639 Bone Collector II
    POJ 3181 Dollar Dayz
    POJ 1787 Charlie's Change
    POJ 2063 Investment
    HDU 1114 Piggy-Bank
    Lca hdu 2874 Connections between cities
  • 原文地址:https://www.cnblogs.com/yinjia/p/5575955.html
Copyright © 2011-2022 走看看