Created
March 8, 2016 14:48
-
-
Save spareslant/cfc6d7bdda99257f5fa2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> pattern = re.compile(r'\w+') | |
>>> pattern.findall('This is a test') | |
['This', 'is', 'a', 'test'] | |
>>> pattern = re.compile(r'(\w+)') | |
>>> pattern.findall('This is a test') | |
['This', 'is', 'a', 'test'] | |
>>> pattern = re.compile(r'\W+') | |
>>> pattern.split("This is a test") | |
['This', 'is', 'a', 'test'] | |
>>> pattern = re.compile(r'(\W+)') | |
>>> pattern.split("This is a test") | |
['This', ' ', 'is', ' ', 'a', ' ', 'test'] | |
>>> pattern = re.compile(r'(\w+)') | |
>>> pattern.search('This is a test') | |
<_sre.SRE_Match object at 0x104cf2cd8> | |
===================================================== | |
>>> re.search(r'\w+', "this is a test") | |
<_sre.SRE_Match object at 0x104cf71d0> | |
>>> re.findall(r'\w+', "this is a test") | |
['this', 'is', 'a', 'test'] | |
>>> re.split(r'\W+', "this is a test") | |
['this', 'is', 'a', 'test'] | |
>>> re.split(r'(\W+)', "this is a test") | |
['this', ' ', 'is', ' ', 'a', ' ', 'test'] | |
>>> re.sub(r'[0-9]+','===','this is a number test 44 66 . will it work') | |
'this is a number test === === . will it work' | |
======================================================= | |
>>> matchobj=re.search(r'(\w+) (\w+)',"hello world. This is test") | |
>>> matchobj.group() | |
'hello world' | |
>>> matchobj.group(1) | |
'hello' | |
>>> matchobj.group(2) | |
'world' | |
>>> matchobj=re.search(r'(?P<first>\w+) (?P<second>\w+)',"hello world. This is test") | |
>>> matchobj.groups() | |
('hello', 'world') | |
>>> matchobj.group() | |
'hello world' | |
>>> matchobj.group('first') | |
'hello' | |
>>> matchobj.groupdict() | |
{'second': 'world', 'first': 'hello'} | |
===================================================== | |
>>> matchobj=re.search(r'\w+ (\w+)',"hello world. This is test") | |
>>> matchobj.group() | |
'hello world' | |
>>> matchobj.start(1) | |
6 | |
>>> matchobj.start() | |
0 | |
>>> matchobj.start(2) | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
IndexError: no such group | |
>>> matchobj.span() | |
(0, 11) | |
>>> matchobj.span(1) | |
(6, 11) | |
=================================== | |
>>> itrobj=re.finditer(r'\w+ (\w+)',"hello world. This is test") | |
>>> m1=itrobj.__next__() | |
>>> m1.group() | |
'hello world' | |
>>> m1.group(1) | |
'world' | |
>>> m1.group(2) | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
IndexError: no such group | |
>>> m2=itrobj.__next__() | |
>>> m2.group() | |
'This is' | |
>>> m2.group(1) | |
'is' | |
>>> m2.group(2) | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
IndexError: no such group | |
>>> m3=itrobj.__next__() | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
StopIteration | |
>>> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment