regular expression examples

import re

a = "java|Python|c0123tttyyyuu9"
res = re.search("java\|Python\|c([0-9]+)tttyyyuu9", a)
print(res.group(0)) # whole string
print(res.groups()) # matched string in ()
print(res.group(1)) # matched string in ()
print(res.span(1)) # span of group 1
print(res.span(1)[0]) # start pos of group1
print(a[res.span(1)[0]]) # start char of matched


a = "java|Pytho|Python|c0123|pythonn|tttyyyuu9"
res = re.search("Python{0,2}", a, re.I)
print(res.group()) # matched string


res = re.search("(Python{0,2})", a, re.I)
print(res.groups())


res = re.findall("Python{0,2}", a, re.I)
print(res)


res = re.findall("Python{0, 2}", a, re.I)
print(res) # space can't add to {}


res = re.findall("(java).*(Python).*(pythonn)", a, re.I)
print(res)
print(res[0][1])

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章