-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_regex_3.py
More file actions
executable file
·47 lines (34 loc) · 949 Bytes
/
Copy pathpython_regex_3.py
File metadata and controls
executable file
·47 lines (34 loc) · 949 Bytes
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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
#
# Karl.Lv@outlook.com, KarlLv@126.com
# 21 August, 2017
import re
pattern_1 = re.compile(r'world')
print "re.search"
match = re.search(pattern_1, 'hello world!')
if match:
print match.group()
pattern = re.compile(r'\d+')
str_1="one1two2three3four4eleven11nine9"
print "re.split"
print re.split(pattern, str_1)
print "re.findall"
print re.findall(pattern, str_1)
print "re.finditer"
for m in re.finditer(pattern, str_1):
print m.group()
pattern_6 = re.compile(r'(\w+) (\w+)')
s = 'i say, hello world!'
print "re.sub"
print re.sub(pattern_6, r'\2 \1', s)
def func(m):
return m.group(1).title() + ' ' + m.group(2).title()
print re.sub(pattern_6, func, s)
pattern_7 = re.compile(r'(\w+) (\w+)')
s = 'i say, hello world!'
print "re.subn"
print re.subn(pattern_7, r'\2 \1', s)
def func(m):
return m.group(1).title() + ' ' + m.group(2).title()
print re.subn(pattern_7, func, s)