wordpress

Split, Sub, Subn functions of re module in Python: Syntax and Usage

Syntax of Split function

The syntax of the split function in the re module in Python is:

re.split(pattern, string, maxsplit=0, flags=0)

Here, the parameters are:

  • pattern: The regular expression pattern to be used for splitting the string.
  • string: The input string that needs to be split.
  • maxsplit: An optional parameter that specifies the maximum number of splits to be made. If not provided, all possible splits are made.
  • flags: An optional parameter that can be used to modify the behavior of the regular expression pattern.

Usage of Split function

The split function in the re module is used to split a string into a list of substrings based on a specified pattern. It uses regular expressions to define the pattern.

Here is an example that demonstrates the usage of the split function:

import re

string = "Hello, World! How are you?"
pattern = r"W+" # Split the string at non-word characters

result = re.split(pattern, string)
print(result)

The output of the above code will be:

['Hello', 'World', 'How', 'are', 'you']

In this example, the split function is used to split the string at non-word characters (characters other than letters, digits, and underscores). The resulting substrings are stored in a list and printed.

Syntax of Sub function

The syntax of the sub function in the re module in Python is:

re.sub(pattern, repl, string, count=0, flags=0)

Here, the parameters are:

  • pattern: The regular expression pattern to be used for finding matches in the string.
  • repl: The replacement string that will be used to replace the matches.
  • string: The input string in which the matches need to be replaced.
  • count: An optional parameter that specifies the maximum number of replacements to be made. If not provided, all matches are replaced.
  • flags: An optional parameter that can be used to modify the behavior of the regular expression pattern.
Recomendado:  Check if a Given Linked List is Circular Linked List - Simple Methods

Usage of Sub function

The sub function in the re module is used to replace occurrences of a pattern in a string with a specified replacement string.

Here is an example that demonstrates the usage of the sub function:

import re

string = "Hello, World! How are you?"
pattern = r"World" # Find occurrences of the word "World"
replacement = "Python" # Replace "World" with "Python"

result = re.sub(pattern, replacement, string)
print(result)

The output of the above code will be:

Hello, Python! How are you?

In this example, the sub function is used to replace occurrences of the word «World» in the string with the word «Python». The resulting string is printed.

Syntax of Subn function

The syntax of the subn function in the re module in Python is:

re.subn(pattern, repl, string, count=0, flags=0)

Here, the parameters are:

  • pattern: The regular expression pattern to be used for finding matches in the string.
  • repl: The replacement string that will be used to replace the matches.
  • string: The input string in which the matches need to be replaced.
  • count: An optional parameter that specifies the maximum number of replacements to be made. If not provided, all matches are replaced.
  • flags: An optional parameter that can be used to modify the behavior of the regular expression pattern.

Usage of Subn function

The subn function in the re module is similar to the sub function, but it also returns the number of replacements made.

Here is an example that demonstrates the usage of the subn function:

import re

string = "Hello, World! How are you?"
pattern = r"o" # Find occurrences of the letter "o"
replacement = "a" # Replace "o" with "a"

result, count = re.subn(pattern, replacement, string)
print(result)
print(count)

The output of the above code will be:

Hella, Warld! Haw are yau?
2

In this example, the subn function is used to replace occurrences of the letter «o» in the string with the letter «a». The resulting string is printed, along with the number of replacements made.

Recomendado:  Convert Python List to NumPy Arrays: Syntax for Conversion

Autor

osceda@hotmail.com

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *