045-001. using regular expressing, re.match(), re.search(), ^, $, |
@
# Regular expression is way to express string which has specific pattern
# So, regular expression is used to search string in specific pattern, and to extract them, and to replace them with other pattern
# And, regular expression is also used to check if some string follows specific rules
@
# match() judges if entire "pattern" is contained in string or not
re.match(pattern, string)
import re
# It judges if 'Hello, world!' contains 'Hello'
re.match('Hello', 'Hello, world!')
# <_sre.SRE_Match object; span=(0, 5), match='Hello'>
# It judges if 'Hello, world!' contains 'Python'
re.match('Python', 'Hello, world!')
@
# search() judges if partial pattern is matched or not
# Since 'Hello, world!' starts with Hello, pattern '^Hello' is matched with 'Hello, world!'
re.search('^Hello', 'Hello, world!')
# <_sre.SRE_Match object; span=(0, 5), match='Hello'>
# Since 'Hello, world!' ends with world!, pattern 'world!$' is matched with 'Hello, world!'
re.search('world!$', 'Hello, world!')
# <_sre.SRE_Match object; span=(7, 13), match='world!'>
@
# We can use | when using match()
# There are hello or world in 'hello', pattern of 'hello|world' is matched with 'hello'
re.match('hello|world', 'hello')
# <_sre.SRE_Match object; span=(0, 5), match='hello'>