- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import re
- # match
- # 方法一pattern1 = re.compile(r'hello', re.I)
- match = pattern1.match('Hello World')
- if match:
- print match.group()
- # 方法二
- m = re.match(r'hello','hello world.')
- print m.group()
- # searchpattern1 = re.compile(r'World')
- match = pattern1.search('Hello, hello World.')
- if match:
- print match.group()
- # splitpattern1 = re.compile(r'\d+')
- match = pattern1.split('one1two2three3')
- print match
- foriin match:
- print i
- # findallmatch = pattern1.findall('one1two2three3')
- print match
- # finditermatch = pattern1.finditer('one1two2three3')
- foriin match:
- printi.group()
来源: