zoukankan      html  css  js  c++  java
  • 快乐的正则一只

      1 # 我 VS 正则
      2 
      3 # 今天我来讲述一下我与正则之间的快乐时光
      4 
      5 # Day1:
      6 
      7 # 首先要导入正则包
      8 import re
      9 
     10 # re.match   从头开始向右匹配        匹配到后就结束
     11 # re.search  从任意位置开始向右匹配  匹配到后就结束
     12 
     13 
     14 # 1. 以 h 开头 (只要是‘h’开头就可以)
     15 line = "htticjs2123"
     16 # line = "ayyehhc"
     17 # 匹配
     18 match_res = re.match('h',line)
     19 # 查看匹配结果
     20 if match_res:
     21     print('匹配成功')
     22     print(match_res)
     23 else:
     24     print('匹配失败')
     25 #--------------------------------------------------
     26 # 2. 以h开头后面跟着一个字符 (只要后面不是空就可以)
     27 
     28 '''
     29 # "." 匹配任意一位字符,除了换行符,当re.DOTALL标记被指定时,则可以匹配包括换行符的任意字符。
     30 
     31 '''
     32 
     33 line = "h tticjs2123"
     34 # line = "ayyehhc"
     35 # 匹配
     36 match_res = re.match('h.',line)
     37 # 查看匹配结果
     38 if match_res:
     39     print('匹配成功')
     40     print(match_res)
     41 else:
     42     print('匹配失败')
     43 
     44 #--------------------------------------------------
     45 # 2.1 . 以h开头后面必须跟着一个 `.`
     46 # line = 'h111111222' #这种情况不该匹配上
     47 line = 'h.22' #这种情况可以匹配上
     48 
     49 
     50 # 反斜杠使我们的第二个特殊字符
     51 match_res = re.match('h.', line)
     52 # 需要匹配的字符串, 能不能符合结果
     53 if match_res:
     54     print('匹配成功')
     55 else:
     56     print('匹配失败')
     57 #--------------------------------------------------
     58 # 2.2 . 以h开头后面必须跟着一个 `\`
     59 
     60 # 'h\' 计算机内部代表的字符串就是 h
     61 line = 'h\'
     62 print(len(line))
     63 # 'h\\' 计算机内部代表的字符串就是 h\
     64 line2 = 'h\\'
     65 print(len(line2))
     66 
     67 line = 'h
    '
     68 print(line)
     69 print(len(line))
     70 # 'h
    ' https://www.processon.com/view/link/5b74cb1ee4b053a09c366d87
     71 match_res = re.match(r'h
    ', line)
     72 if match_res:
     73     print('匹配成功')
     74     print(match_res)
     75 else:
     76     print('匹配失败')
     77 #--------------------------------------------------
     78 line = r'h\'
     79 print(len(line))
     80 
     81 # 反斜杠使我们的第二个特殊字符
     82 # 当匹配的时候, 字符串内部的 被当做转义字符, h\能够匹配的字符串就是h
     83 match_res = re.match('h\\', line)
     84 # 需要匹配的字符串, 能不能符合结果
     85 if match_res:
     86     print('匹配成功')
     87 else:
     88     print('匹配失败')
     89 #--------------------------------------------------
     90 # 2.3 以h开头后面只跟着一个字符
     91 line = 'h2k'
     92 # $ 特殊字符.
     93 match_res = re.match('h.', line)
     94 # 需要匹配的字符串, 能不能符合结果
     95 if match_res:
     96     print('匹配成功')
     97 else:
     98     print('匹配失败')
     99 #--------------------------------------------------
    100 # 3. 以h开头后面跟着任意数量的数字
    101 '''
    102 # "d" 匹配任意数字,等价于 [0-9].
    103 # "*"  匹配0个或多个的表达式
    104 
    105 '''
    106 
    107 line = "h2123xxd"
    108 # line = "ayyehhc"
    109 # 匹配
    110 match_res = re.match('hd.*',line)
    111 # 查看匹配结果
    112 if match_res:
    113     print('匹配成功')
    114     print(match_res)
    115 else:
    116     print('匹配失败')
    117 #--------------------------------------------------
    118 # 4. 以3结尾
    119 
    120 '''
    121 # "$" 匹配以该符号前面一个字符结尾的字符串
    122 
    123 '''
    124 
    125 line = "htticjs2123"
    126 # line = "ayyehhc"
    127 # 匹配
    128 match_res = re.match('.*3$',line)
    129 # 查看匹配结果
    130 if match_res:
    131     print('匹配成功')
    132     print(match_res)
    133 else:
    134     print('匹配失败')
    135 #--------------------------------------------------
    136 # 5. 以h开头,以3结尾,中间只有一个字符
    137 
    138 
    139 line = "h23"
    140 # ^ 代表着以这里开头儿
    141 # 匹配  'h.3$' '^h.3$'
    142 match_res = re.match('^h.3$',line)
    143 # 查看匹配结果
    144 if match_res:
    145     print('匹配成功')
    146     print(match_res)
    147 else:
    148     print('匹配失败')
    149 #--------------------------------------------------
    150 # 6. 以h开头,以3结尾,中间可以存在任意数量的字符串
    151 line = "htticjs2123"
    152 
    153 # 匹配  'h.*3$' '^h.*3$'
    154 match_res = re.match('h.*3$',line)
    155 # 查看匹配结果
    156 if match_res:
    157     print('匹配成功')
    158     print(match_res)
    159 else:
    160     print('匹配失败')
  • 相关阅读:
    Balance的数学思想构造辅助函数
    1663. Smallest String With A Given Numeric Value (M)
    1680. Concatenation of Consecutive Binary Numbers (M)
    1631. Path With Minimum Effort (M)
    1437. Check If All 1's Are at Least Length K Places Away (E)
    1329. Sort the Matrix Diagonally (M)
    1657. Determine if Two Strings Are Close (M)
    1673. Find the Most Competitive Subsequence (M)
    1641. Count Sorted Vowel Strings (M)
    1679. Max Number of K-Sum Pairs (M)
  • 原文地址:https://www.cnblogs.com/daihao9527/p/9490903.html
Copyright © 2011-2022 走看看