Character Groups Within Sets in Python Regular Expressions
The character groups \d
, \D
, \w
, \W
, \s
, \S
inside []
will denote precisely the groups, that is, they will still be commands.
Example
In this example, the search template looks like this: between the x's there is any number or letter from 'a'
to 'f'
:
txt = 'xax xbx x1x x2x xhx x@x'
res = re.sub('x[\da-f]x', '!', txt)
print(res)
Result of code execution:
'! ! ! ! xhx x@x'
Example
In this example, the search pattern looks like this: letter 'x'
, then not a number, not a period, and not a lowercase Latin letter, then letter 'z'
:
txt = 'xaz x1z xAz x.z x@z'
res = re.sub('x[^\d.a-z]z', '!', txt)
print(res)
Result of code execution:
'xaz x1z ! x.z !'
Practical tasks
Write a regular expression that will find strings with the pattern: digit or dot 1
or more times.
Write a regular expression that will find lines according to the pattern: neither a number nor a letter from 'a'
to 'g'
from 3
to 7
times.