re

# -*- coding: utf-8 -*-
__author__ = 'scott'

import re

line = "boby123"

if line == "boby123":
    print("equal:ture")

# .表示任意字符 *代表前面那个字符重复次数 ^以..开头  $以..结尾 ?非贪婪匹配的模式(取消非贪婪,从左边开始找),默认贪婪模式,匹配最大长度

reg_str = "^b.*3$"

if re.match(reg_str,line):
    print("mathced")
else:
    print("not matched")

line2 = "boooooobbay123"

match_str = ".*?(b.*?b).*"
match_obj = re.match(match_str,line2)

if match_obj:
    print(match_obj.group(1)) #boooooob

# + 至少出现一次

line3 ="boooobbby123"
match_str = ".*(b.+b).*"
if re.match(match_str,line3):
    print(re.match(match_str,line3).group(1)) #bbb

# {2,} 等于2次  {2,} 大于等于2次  {2,5} 大于等于2,小于等于5次 |或者   ()group,左->右 1开始编号

line4 = "bobby123"
match_str = "((boby|bobby)123)"
match_obj = re.match(match_str,line4)
if match_obj:
    print(match_obj.group(1)) #bobby123
    print(match_obj.group(2)) #bobby

# [] eg: [abcd] 表示abcd中任意一个字符 [0-9][0-9a-zA-Z] 注意[]只表示一个字符  [^1]{9} 不等于1,出现9次 [.*] 进入中括号.*就没右特殊含义了,就是.或*

line5 = "bobby123"
match_str = "([abcd]obby123)"
match_obj = re.match(match_str,line4)
if match_obj:
    print(match_obj.group(1))# bobby123

# \s空格   |S 只要不是空格

line6 = "你 好"
match_str = "(你\s好)"
match_obj = re.match(match_str,line6)
if match_obj:
    print(match_obj.group(1)) # 你 好

line6 = "你sa好"
match_str = "(你\S+好)"
match_obj = re.match(match_str,line6)
if match_obj:
    print(match_obj.group(1)) # 你sa好

#  \w 相当于[0-9A-Za-z_]  \W [^0-9A-Za-z_]

# [\u4E00-\u9FA5] 表示汉字

line6 = "你 好s"
match_str = "([\u4E00-\u9FA5]+)"
match_obj = re.match(match_str,line6)
if match_obj:
    print(match_obj.group(1)) # 你

line6 = "study in 南京大学"
match_str = ".*?([\u4E00-\u9FA5]+大学)"
match_obj = re.match(match_str,line6)
if match_obj:
    print(match_obj.group(1)) # 南京大学

# \d 表示数字 = [0-9]

line6 = "xxx出生于2012年"
match_str = ".*?(\d+)年"
match_obj = re.match(match_str,line6)
if match_obj:
    print(match_obj.group(1)) # 2012
    111

results matching ""

    No results matching ""