Menu Close

Replacing strings using Regular Expression Regex

Basics of Regular Expressions in Python, check the link below πŸ‘‡ πŸ‘‡
Regular Expressions Regex basic in Python πŸ‘ˆ πŸ‘ˆ

Also with table of contents about symbols and their usage in Regex

Links for other methods like search, flags and pre-compiled patterns in regular expression scroll down
πŸ‘‡ πŸ‘‡

Python makes regular expressions available through the re module.

Replacing in Regex Python

Replacements can be made on strings using re.sub .

Replacing strings

import re
print( re.sub(r"t[0-9][0-9]", "foo", "my name t13 is t44 what t99 ever t44") )

# Output: 'my name foo is foo what foo ever foo'

OUTPUT: my name foo is foo what foo ever foo

Using group references
Replacements with a small number of groups can be made as follows:

import re
print( re.sub(r"t([0-9])([0-9])", r"t\2\1", "t13 t19 t81 t25") )

# Output: 't31 t91 t18 t52'

OUTPUT: t31 t91 t18 t52

However, if you make a group ID like ’10’, this doesn’t work: \10 is read as ‘ID number 1 followed by 0’. So you have to be more specific and use the \g notation:

import re
print( re.sub(r"t([0-9])([0-9])", r"t\g<2>\g<1>", "t13 t19 t81 t25") )

# Output: 't31 t91 t18 t52'

OUTPUT: t31 t91 t18 t52

Using a replacement function

import re
items = ["zero", "one", "two"]
print( re.sub(r"a\[([0-3])\]", lambda match: items[int(match.group(1))], "Items: a[0], a[1], something, a[2]") )
       
# Output: 'Items: zero, one, something, two'

OUTPUT: Items: zero, one, something, two

ILLUSTRATION USING PYTHON3 CLI

Executed using python3 linux terminal

For the first part in using regular expressions and matching the string
you can visit this link –>   πŸ‘‡ πŸ‘‡
Matching the beginning of a string (Regex) Regular Expressions in python

The re.search() method takes a regular expression pattern and a string and searches for that pattern within the string. If the search is successful, search() returns a match object or None otherwise.
For the second part in using regular expression and searching the string
visit –> Searching – Regular Expressions (Regex) in Python

Precompiled_pattern β€”> πŸ‘‡ πŸ‘‡
 Precompiled patterns – Regular Expression(Regex) in Python
Compiling a pattern allows it to be reused later on in a program.
However, note that Python caches recently-used expressions, so β€œprograms that use only a few regular expressions at a time needn’t worry about compiling regular expressions”.

Flags in Regex πŸ‘‰ πŸ‘‰ Flags in Regular Expressions ( Regex ) in Python….FTC
For some special cases we need to change the behavior of the Regular Expression, this is done using flags. Flags can be set in two ways, through the flags keyword or directly in the expression.


Morae Q!

  1. Program to get dictionary items, use update module and items module.
  2. Regular expression allowing certain set of characters.
  3. Compute the Sum of Cosine Series.
  4. Find the Sum of Sine Series.
  5. Remove the Vowels from the string.
  6. Find All Non-Overlapping strings/characters using regular expressions .
  7. Check whether the given string is Panagram or not.
  8. Replacing strings using Regular Expression Regex.
  9. Check whether the given number is Strong number or not.
  10. Find the numbers divisible by a input numbers within the given range.
  11. Change the behaviour of the Regular Expression using flags.
  12. Find the exponentiation of a number.
  13. Find all prime numbers in a range using Sieve of Eratosthenes.
  14. Convert decimal equivalent into binary, hexadecimal and octal.
  15. Compute the area of a farmers field in feet.
  16. Compute time from number of days/hours to seconds.
  17. Precompiled patterns using Regular Expressions .
  18. Compute the price of the cake on the nth day.
  19. Compute the number of possible non-increasing arrays.
  20. Find the type of spinach using the calorie value.