Character Sets in Python Regular Expressions
The character groups \d and \w are not very flexible. Even such a simple task as find all letters but not digits - cannot be solved by them. For such tasks, square brackets should be used, which represent the operation ' or '.
Square brackets replace one character, any of those listed inside. For example, like this: x[abc]x - we say that there should be letters X on the edges, and one character inside: either 'a', or 'b', or 'c'.
After square brackets, you can write repetition operators. For example, like this: x[abc]+x - we say that inside the x's there can be any number of symbols 'a', 'b' and 'c' - in any combinations.
You can not only list characters, but also create character groups by writing a hyphen between two characters. For example, like this: [a-d] - we get all characters from 'a' to 'd'.
Let's look at some examples.
Example
In this example, the search template looks like this: between the x's any letter from 'a' to 'z':
Result of code execution:
'! ! ! x@x'
Example
In this example, the search template looks like this: between the x's any letter from 'a' to 'k':
txt = 'xax xbx xmx x@x'
res = re.sub('x[a-k]x', '!', txt)
print(res)
Result of code execution:
'! ! xmx x@x'
Example
In this example, the search template looks like this: between the x's any letter from 'A' to 'Z':
txt = 'xax xBx xcx x@x'
res = re.sub('x[A-Z]x', '!', txt)
print(res)
Result of code execution:
'xax ! xcx x@x'
Example
In this example, the search pattern looks like this: between x's any digit from 0 to 9:
txt = 'xax x1x x3x x5x x@x'
res = re.sub('x[0-9]x', '!', txt)
print(res)
Result of code execution:
'xax ! ! ! x@x'
Example
In this example, the search pattern looks like this: between x's any digit from 3 to 7:
txt = 'xax x1x x3x x5x x@x'
res = re.sub('x[3-7]x', '!', txt)
print(res)
Result of code execution:
'xax x1x ! ! x@x'
Example
In this example, the search template looks like this: between the x's any letter from 'a' to 'z' or a number from 1 to 9:
txt = 'xax x1x x3x x5x x@x'
res = re.sub('x[a-z1-9]x', '!', txt)
print(res)
Result of code execution:
'! ! ! ! x@x'
Example
In this example, the search template looks like this: between the x's any letter from 'a' to 'z' or a letter from 'A' to 'Z':
txt = 'xax xBx xcx x5x x@x'
res = re.sub('x[a-zA-Z]x', '!', txt)
print(res)
Result of code execution:
'! ! ! x5x x@x'
Example
In this example, the search template looks like this: between the x's any letter from 'a' to 'z' or numbers 1, 2:
txt = 'xax xbx x1x x2x x3x'
res = re.sub('x[a-z12]x', '!', txt)
print(res)
Result of code execution:
'! ! ! ! x3x'
Example
In this example, the search pattern looks like this: between x's letters from 'a' to 'z' in quantity from 1 and more:
txt = 'xx xabesx xaadx x123x xa3x'
res = re.sub('x[a-z]+x', '!', txt)
print(res)
Result of code execution:
'xx ! ! ! x123x xa3x'
Example
Let's make it so that the number of letters can be zero:
txt = 'xx xabesx xaadx x123x xa3x'
res = re.sub('x[a-z]*x', '!', txt)
print(res)
As a result we get:
'! ! ! ! x123x xa3x'
Example
You can also specify escaped characters inside square brackets. Let's find a sequence of letters and numbers that repeats zero or more times:
txt = 'xx x@x xadx xas12x xa3x'
res = re.sub('x[a-z\d]*x', '!', txt)
print(res)
Result of code execution:
'! x@x ! ! !'
Example
If we need to specify more square brackets, we must also escape them:
txt = 'xx xrx xas[]x x3x'
res = re.sub('x[a-z\[\]]*x', '!', txt)
print(res)
Result of code execution:
'! ! ! x3x'
Practical tasks
Given a string:
txt = 'aba aea aca aza axa'
Write a regular expression that will match the following pattern: on the edges are the letters 'a', and between them is the letter 'b', 'e' or 'x'.
Given a string:
txt = 'a1a a3a a7a a9a aba'
Write a regular expression that will match the following pattern: on the edges there are letters 'a', and between them there is a number from 3 to 6.
Given a string:
txt = 'aba aea afa aha aga'
Write a regular expression that will match the following pattern: on the edges are the letters 'a', and between them is a letter from 'a' to 'g'.
Given a string:
txt = 'aba aea afa aha aga'
Write a regular expression that will match the following pattern: on the edges are the letters 'a', and between them is a letter from 'a' to 'f' and from 'j' to 'z'.
Given a string:
txt = 'aAa aea aEa aJa a3a'
Write a regular expression that will match the following pattern: on the edges are the letters 'a', and between them is a letter from 'a' to 'f' and from 'A' to 'D'.
Given a string:
txt = 'aAXa aeffa aGha aza ax23a a3sSa'
Write a regular expression that will match the following pattern: on the edges are the letters 'a', and between them are small Latin letters, without affecting the rest.
Given a string:
txt = 'aAXa aeffa aGha aza ax23a a3sSa'
Write a regular expression that will match the following pattern: on the edges are the letters 'a', and between them are small and large Latin letters, without affecting the rest.
Given a string:
txt = 'aAXa aeffa aGha aza ax23a a3sSa'
Write a regular expression that will match the following pattern: on the edges are the letters 'a', and between them are small Latin letters and numbers, without affecting the rest.