Regular Expressions - Practice exercises
You can do all the following exercises for a single string.
Source : https://www.w3resource.com/python-exercises/re/
It's done as a string of words to show what type of strings would and would not be matched.
Remove the \b when feeding a single word in a string.
\b : the empty string, but only at the start or end of the word
Specify in raw (r) format to avoid the clash of \ as an escape and \ to give meaning in Python
>>> s = 'a ab abb b ababa'
>>> import re
>>> re.findall(r'\bab*',s)
['a', 'ab', 'abb', 'ab']
>>> s = 'a ab abb b abbbb abbb‘
>>> re.findall(r'\babbb\b',s)
['abbb']
>>> re.findall(r'\bab{3}\b',s)
['abbb']
Source : https://www.w3resource.com/python-exercises/re/
It's done as a string of words to show what type of strings would and would not be matched.
Remove the \b when feeding a single word in a string.
\b : the empty string, but only at the start or end of the word
Specify in raw (r) format to avoid the clash of \ as an escape and \ to give meaning in Python
Note: \b and \B definitions depends on \w and \W
It is a boundary between a \w and a \W
1. String that has an a followed by zero or more b's
there should be an a followed b 0 or more b's>>> s = 'a ab abb b ababa'
>>> import re
>>> re.findall(r'\bab*',s)
['a', 'ab', 'abb', 'ab']
2. a string that has an a followed by three 'b'.
you can use the {m} to specify the amount of times the preceding character/special character should be repeated.>>> s = 'a ab abb b abbbb abbb‘
>>> re.findall(r'\babbb\b',s)
['abbb']
>>> re.findall(r'\bab{3}\b',s)
['abbb']
3. one upper case letter followed by lower case letters
set [] matches a single character within.
Hyphen - between 2 characters specify a range
Hyphen in beginning or end just matches it literally
>>> s= 'Awesome great Wow'
>>> re.findall(r'\b[A-Z][a-z]*\b',s)
['Awesome', 'Wow']
4. string that has an 'a' followed by anything, ending in 'b‘
s='abbbccb bbbaccb ab'
>>> re.findall(r'\ba.*?b\b',s)
['abbbccb', 'ab']
5. matches a word containing 'z‘
\w used to match a word either in the beginning or end or separated by set of whitespace characters
>>> s = 'bizzare zebra running wild‘
>>> re.findall(r'\w*z\w*',s)
['bizzare', 'zebra']
6. matches a word containing 'z', not at the start or end of the word
This demonstrate the use of \B which is the opposite of \b
This matches a string but not at the start or end of a word.
>>> s='zebra‘
>>> re.search(r'\Bz\B',s) # no match
>>> s='bizzare'
>>> re.search(r'\Bz\B',s)
<re.Match object; span=(2, 3), match='z'>
Comments
Post a Comment