pythonregex
Ben Gorman

Ben Gorman

Life's a garden. Dig it.

Here's a quote from Star Wars: Episode II - Attack of the Clones :material-death-star:

quote = """You wanna buy 20 death sticks for 8 dollars?
You don't want to sell me 20 death sticks.
How about 120 death sticks for 80 dollars?
No.
9 death sticks for 2 dollars?
Yeah I'll go for that."""

Alter the quote so that 20 death sticks, 120 death sticks, and 9 death sticks become some death sticks.

Expected result

newquote = """You wanna buy some death sticks for 8 dollars?
You don't want to sell me some death sticks.
How about some death sticks for 80 dollars?
No.
some death sticks for 2 dollars?
Yeah I'll go for that."""

Regex Functions

Function Description Return Value
re.findall(pattern, string, flags=0) Find all non-overlapping occurrences of pattern in string list of strings, or list of tuples if > 1 capture group
re.finditer(pattern, string, flags=0) Find all non-overlapping occurrences of pattern in string iterator yielding match objects
re.search(pattern, string, flags=0) Find first occurrence of pattern in string match object or None
re.split(pattern, string, maxsplit=0, flags=0) Split string by occurrences of pattern list of strings
re.sub(pattern, repl, string, count=0, flags=0) Replace pattern with repl new string with the replacement(s)

Regex Patterns

Pattern Description
[abc] a or b or c
[^abc] not (a or b or c)
[a-z] a or b ... or y or z
[1-9] 1 or 2 ... or 8 or 9
\d digits [0-9]
\D non-digits [^0-9]
\s whitespace [ \t\n\r\f\v]
\S non-whitespace [^ \t\n\r\f\v]
\w alphanumeric [a-zA-Z0-9_]
\W non-alphanumeric [^a-zA-Z0-9_]
. any character
x* zero or more repetitions of x
x+ one or more repetitions of x
x? zero or one repetitions of x
{m} m repetitions
{m,n} m to n repetitions
{m,n} m to n repetitions
\\, \., \* backslash, period, asterisk
\b word boundary
^hello starts with hello
bye$ ends with bye
(...) capture group
(po|go) po or go

Solution

import re
 
newquote = re.sub(
    pattern='\d+ death sticks', 
    repl="some death sticks", 
    string=quote
)
 
print(newquote)
# You wanna buy some death sticks for 8 dollars?
# You don't want to sell me some death sticks.
# How about some death sticks for 80 dollars?
# No.
# some death sticks for 2 dollars?
# Yeah I'll go for that.

Explanation

  1. \d matches a single digit
  2. \d+ matches one or more digits
  3. \d+ death sticks matches one or more digits follow by " death sticks"

re.sub(pattern, repl, string, ...) returns the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl.

newquote = re.sub(
    pattern='\d+ death sticks', 
    repl="some death sticks",   
    string=quote
)
 
print(newquote)
# You wanna buy some death sticks for 8 dollars?
# You don't want to sell me some death sticks.
# How about some death sticks for 80 dollars?
# No.
# some death sticks for 2 dollars?
# Yeah I'll go for that.