Repository navigation
Expand file tree
/
Copy pathRegular1.py
More file actions
93 lines (83 loc) · 2.47 KB
/
Copy pathRegular1.py
File metadata and controls
93 lines (83 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# -*- coding: utf-8 -*-
#---------------------------------------
# 程序:YouTube:正则表达式学习--正则表达式符号与方法
# 常用符号: . * ? ()
# 常用方法: findall,search,sub
# 网址:https://www.youtube.com/watch?v=Tt6JZfrZn0Q
# 日期:2016-08-26
# 语言:Python 2.7
#---------------------------------------
# 密码:ugsdsxxIxx123xxlovexxfdhfhuidxxyouxxhufhudh
#匹配纯数字,用\d+,当然用(.*?)也没问题
import re
a='asbj44sdkfhsf12324kdj99gkj'
b=re.findall('(\d+)',a)
# #['44', '12324', '99']
b2=re.search('(\d+)',a).group(1)
print b2
# ## compile的用法,但是不推荐这么用
# #与这个类似:# f1=findall('xx(.*?)xx',s2,S)
# import re
# s2 = 'ugsdsxxIxx123xxlovexxfdhfhuidxxyouxxhufhudh'
# pattern='xx(.*?)xx'
# new_pattern=re.compile(pattern,re.S)
# output=re.findall(new_pattern,s2)
# ['I', 'love', 'you']
# print output
# import re
# from re import findall,search,S
# #可以省掉re.,但是不推荐这么做,还是保留re.S
# s2='ugsdsxxIxx123xxlovexxfdhfhuidxxyouxxhufhudh'
# f0=search('xx(.*?)xx123xx(.*?)xx',s2).group(0)
# #xxIxx123xxlovexx
# f1=findall('xx(.*?)xx',s2,S)
# #['I', 'love', 'you']
# print f1
# import re
# s2='ugsdsxxIxx123xxlovexxfdhfhuidxxyouxxhufhudh'
#
# s='123abcssfdf123'
# #output=re.sub('123(.*?)123','123789123',s)
# #123789123
# #output=re.sub('123(.*?)123','123%d123'%888,s)
# #123888123
# output=re.sub('123(.*?)123','test',s)
# #test
# print output
# search 要跟group()
# f00=re.search('xx(.*?)xx123xx(.*?)xx',s2)
# # <_sre.SRE_Match object at 0x02586728>
#
# f0=re.search('xx(.*?)xx123xx(.*?)xx',s2).group(0)
# #xxIxx123xxlovexx
#
# f1=re.search('xx(.*?)xx123xx(.*?)xx',s2).group(1)
# #I
#
# f2=re.search('xx(.*?)xx123xx(.*?)xx',s2).group(2)
# #love
#
# #f3=re.search('xx(.*?)xx123xx(.*?)xx',s2).group(3)
# # AttributeError: 'list' object has no attribute 'group'
#
# f4=re.findall('xx(.*?)xx123xx(.*?)xx',s2)
# # [('I', 'love')]
#
# f5=re.findall('xx(.*?)xx123xx(.*?)xx',s2)
# #print f5[0][0]
# # I
# print f5[0][1]
# #love
# secort_code='ugsdsxxIxxsfjhfdkhxxlovexxfdhfhuidxxyouxxhufhudh'
# s='''ugsdsxxIxxsfjhfdkhxxlove
# xxfdhfhuidxxyouxxhufhudh
# '''
# # (.*?) 需要掌握,需要的内容,就用()包围起来,不需要的就放()外面
# #b=re.findall('xx.*?xx',secort_code)
# # ['xxIxx', 'xxlovexx', 'xxyouxx']
# # b=re.findall('xx(.*?)xx',secort_code)
# # ['I', 'love', 'you']
# b=re.findall('xx(.*?)xx',s,re.S)
# #re.S 让匹配内容,包含\n
# print b
# ['I', 'love\n', 'you']