C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Sub: In the main program body, we call the re.sub method. The first argument is "\d+" which means one or more digit chars.
And: The second argument is the multiply method name. We also pass a sample string for processing.
Result: The re.sub method matched each group of digits (each number) and the multiply method doubled it.
Python program that uses re.sub
import re
def multiply(m):
# Convert group 0 to an integer.
v = int(m.group(0))
# Multiply integer by 2.
# ... Convert back into string and return it.
return str(v * 2)
# Use pattern of 1 or more digits.
# ... Use multiply method as second argument.
result = re.sub("\d+", multiply, "10 20 30 40 50")
print(result)
Output
20 40 60 80 100
Python program that uses string replacement
import re
# An example string.
v = "running eating reading"
# Replace words starting with "r" and ending in "ing"
# ... with a new string.
v = re.sub(r"r.*?ing", "ring", v)
print(v)
Output
ring eating ring
Tip: If you must know the number of substitutions made by re.sub, using re.subn is an ideal choice.
However: If your program has no use of this information, using re.sub is probably best. It is simpler and more commonly used.
Python program that calls re.subn
import re
def add(m):
# Convert.
v = int(m.group(0))
# Add 2.
return str(v + 1)
# Call re.subn.
result = re.subn("\d+", add, "1 2 3 4 5")
print("Result string:", result[0])
print("Number of substitutions:", result[1])
Output
Result string: 11 21 31 41 51
Number of substitutions: 5
Here: We add the string "ing" to the end of all words within the input string. Additional logic could be used to make the results better.
Tip: A gerund form of a verb cannot be made this way all the time. Sometimes other spelling changes are needed.
Python program that uses re.sub, lambda
import re
# The input string.
input = "laugh eat sleep think"
# Use lambda to add "ing" to all words.
result = re.sub("\w+", lambda m: m.group(0) + "ing", input)
# Display result.
print(result)
Output
laughing eating sleeping thinking
Here: We replace all known "plant" strings with the string PLANT. On other words, modify() takes no action.
Python program that uses re.sub with dictionary
import re
plants = {"flower": 1, "tree": 1, "grass": 1}
def modify(m):
v = m.group(0)
# If string is in dictionary, return different string.
if v in plants:
return "PLANT"
# Do not change anything.
return v
# Modify to remove all strings within the dictionary.
result = re.sub("\w+", modify,
"bird flower dog fish tree")
print(result)
Output
bird PLANT dog fish PLANT