In [1]:
import re

Replace the string "foo" when it's got non-words or line boundaries to the left and to the right


In [2]:
re.sub(r'(?:\W|^)foo(?:\W|$)',' FOO ','foo bar foo foofoo barfoobar foo')


Out[2]:
' FOO bar FOO foofoo barfoobar FOO '

String contains regex


In [3]:
# contains
re.search('\d{3}','foo 123 bar')


Out[3]:
<_sre.SRE_Match object; span=(4, 7), match='123'>

In [4]:
# doesn't contain
re.search('\d{3}','foo 1 23 bar')

extract group

extract first coccurrence of regex in string

only the FIRST match is returned!


In [20]:
# letter 'b' followed by two characters or numbers
pattern = r'b\w{2}'

match = re.search(pattern,"foo bar baz quux")
match


Out[20]:
<_sre.SRE_Match object; span=(4, 7), match='bar'>

In [21]:
match.group(0)


Out[21]:
'bar'

extract regex multiple times


In [6]:
pattern = r'b\w{2}'
re.findall(pattern,'foo bar baz quux')


Out[6]:
['bar', 'baz']

split string by regex


In [7]:
re.split('[\s,]+','foo,bar bar  quux')


Out[7]:
['foo', 'bar', 'bar', 'quux']

split string by word boundary


In [39]:
re.split(r'\b','foo,bar   bar-quux')


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-39-b52c97da0252> in <module>
----> 1 re.split(r'\b','foo,bar  bar-quux')

~/venv36/lib/python3.6/re.py in split(pattern, string, maxsplit, flags)
    210     and the remainder of the string is returned as the final element
    211     of the list."""
--> 212     return _compile(pattern, flags).split(string, maxsplit)
    213 
    214 def findall(pattern, string, flags=0):

ValueError: split() requires a non-empty pattern match.

In [40]:
# only word characters
re.findall(r'\w+','foo,bar   bar-quux')


Out[40]:
['foo', 'bar', 'bar', 'quux']

In [42]:
re.findall(r'\w+|[^\w\s]+','foo,bar   bar-quux')


Out[42]:
['foo', ',', 'bar', 'bar', '-', 'quux']

non capturing groups

lookbehind


In [8]:
pattern = "(?<=foo)bar"
replacement = "BAR"
string = "foo bar foobar"

re.sub(pattern,replacement,string)


Out[8]:
'foo bar fooBAR'

negative lookbehind


In [9]:
pattern = "(?<!foo)bar"
replacement = "BAR"
string = "foo bar foobar"

re.sub(pattern,replacement,string)


Out[9]:
'foo BAR foobar'

lookahead


In [10]:
pattern = "foo(?=bar)"
replacement = "FOO"
string = "foo bar foobar"

re.sub(pattern,replacement,string)


Out[10]:
'foo bar FOObar'

negative lookahead


In [11]:
pattern = "foo(?!bar)"
replacement = "FOO"
string = "foo bar foobar"

re.sub(pattern,replacement,string)


Out[11]:
'FOO bar foobar'

In [12]:
re.match('abc','xx abc xx')

In [13]:
re.match('abc','abc')


Out[13]:
<_sre.SRE_Match object; span=(0, 3), match='abc'>

In [14]:
match = re.search('abc','xx abc xx')
match


Out[14]:
<_sre.SRE_Match object; span=(3, 6), match='abc'>

In [15]:
match = re.search('abc','xx abc xx abc')
match


Out[15]:
<_sre.SRE_Match object; span=(3, 6), match='abc'>

full match


In [16]:
re.fullmatch('foo\d+','foo123')


Out[16]:
<_sre.SRE_Match object; span=(0, 6), match='foo123'>

In [17]:
re.fullmatch('foo\d+','foo123bar')

In [18]:
re.match('^foo\d+$','foo123')


Out[18]:
<_sre.SRE_Match object; span=(0, 6), match='foo123'>

In [19]:
re.match('^foo\d+$','foo123bar')

In [ ]:


In [ ]:


In [ ]: