-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregex_basic.py
More file actions
30 lines (25 loc) · 869 Bytes
/
regex_basic.py
File metadata and controls
30 lines (25 loc) · 869 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
# -*- coding: utf-8 -*-
# python
import re
# match string using re.search
xx=re.search(r" (\w+@\w+\.com) ", "this xyz@example.com that")
if xx:
print xx.group(1)
else:
print "no"
## replace using re.sub
myText = r"""<p><img src="./rabbits.gif" width="30" height="20">
and <img class="xyz" src="../cats.gif">,
but <img src ="tigers.gif">,
<img src=
"bird.gif">!</p>"""
# using regex to replace the content, notice it contain captured pattern (the \1)
newText = re.sub(r'src\s*=\s*"([^"]+)\.gif"', r'src="\1.png"', myText)
print newText
## split string line using re.split
myText = ur"""你是我最苦澀的等待 | you are my hardest wait
讓我歡喜又害怕未來 | giving me joy and also fear the future"""
myLines=re.split(r'\n', myText)
for aLine in myLines:
linesParts=re.split(r'\s*|\s*', aLine, re.U)
print linesParts[0].encode('utf-8')