regex re

Published by onesixx on

s = 'onesixx'

pattern = re.compile('sixx')
result = pattern.match(s)

result = re.match(pattern, s)   # same above

# -----------------

re.match('x', s)
# None
re.fullmatch('onesixx', s)
# 

re.search('x', s)
# 

re.findall('x', s)
# ['x', 'x']

re.finditer('x', s)
# 
for mobj in re.finditer('x', s):
    print(mobj)
# 
# 


# -------Match Object의 메소드 ----------

for mobj in re.finditer('x', s):
    print(f'group: {mobj.group()}')
    print(f'start: {mobj.start()}')
    print(f'end:   {mobj.end()}')
    print(f'span:  {mobj.span()}') 
    
# group: x
# start: 5
# end:   6
# span:  (5, 6)
# group: x
# start: 6
# end:   7
# span:  (6, 7)
exList = ['iiiabcuuu', 'abcooo', 'abcppp', '123ttt', '123yyy']

[s for s in exList if "abc" in s]        
# ['iiiabcuuu', 'abcooo', 'abcppp']

[s for s in exList if re.match("abc", s)]
# ['abcooo', 'abcppp']
Categories: Python Basic

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x