Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Beyond searching and matching, text processing pipelines frequently require transforming, sanitizing, redacting, and tokenizing text data. In Python, the re module provides robust primitives for these workflows through re.sub(), re.subn(), and re.split().
By utilizing backreferences, dynamic replacement callables, and delimiter-capturing splits, developers can build production-grade text transformation engines.
re.sub and BackreferencesThe re.sub(pattern, replacement, string, count=0) function replaces occurrences of a pattern with a replacement string:
\1, \2, \3 refer to positional captured groups.\g<name> refers to named captured groups (?P<name>...). Using \g<1> is also safer than \1 when the replacement string is immediately followed by a literal digit (e.g. \g<1>0 avoids being parsed as group 10).repl)When the replacement value depends on computation, database lookup, or conditional logic, re.sub() accepts a callable that receives a Match object and returns a replacement string:
re.subnre.subn() performs the exact same substitution as re.sub(), but returns a tuple containing the modified string and the total number of substitutions performed:
Template engines like Jinja2 or microservice config injectors use callable replacements to populate variables dynamically:
How data and code flow step-by-step
re.splitStandard str.split() only splits on fixed substrings. re.split() splits on complex regex patterns:
re.split(r"([,;])", text), Python retains and includes the delimiters themselves in the output list! Use non-capturing groups (?:...) to avoid including delimiters.| Method | Role | Return Value | Special Parameter |
|---|---|---|---|
re.sub(pat, repl, s) | Pattern-based substitution | str (transformed string) | repl can be string or callable |
re.subn(pat, repl, s) | Substitution with count | (str, int) | Returns total substitutions made |
re.split(pat, s) | Tokenization on pattern | list[str] | Capturing groups include delimiters |
\g<name> | Named backreference | Evaluates to group in replacement | Replaces captured group by identifier |
re.sub Replacement StringsBackslashes in replacement strings are treated as escape sequences or group references (\1). To write literal backslashes, escape them or use raw strings.
re.splitSplitting strings by regex patterns that match at string boundaries can produce leading or trailing empty strings ("") in the result list.
Use re.sub() with a callback replacement function to mask credit card numbers in text, keeping only the last 4 digits visible.
What syntax is used in re.sub() replacement strings to reference a named capturing group (?P<user>\w+)? A. \user B. \g<user> C. $user D. {user}
\g<group_name> syntax.What is the return type of re.subn(pattern, repl, text)? A. A single modified string. B. A tuple containing (modified_string, substitution_count). C. A dictionary mapping old strings to new strings. D. An integer count of matches.
re.subn() returns a 2-tuple: the transformed string and an integer indicating how many substitutions were made.What occurs when re.split() is executed with capturing parentheses in the pattern, such as re.split(r"([;:])", text)? A. Python raises a ValueError. B. The delimiters that matched the pattern are retained and included as elements in the resulting list. C. Delimiters are converted into empty strings. D. Only the first split is performed.
re.split(), the matched delimiter substrings are preserved and inserted into the resulting list of tokens.What argument does Python pass to a callable function provided as the repl argument in re.sub(pattern, my_func, text)? A. The entire original text string. B. A re.Match object representing the current match. C. An integer index of the match. D. A list of characters.
repl is a callable, re.sub() invokes it for each non-overlapping match, passing the active re.Match object as its sole argument.Why is \g<1>0 preferred over \10 when replacing a group with captured group 1 followed by a literal zero? A. \10 is a syntax error in Python. B. \10 is interpreted by the regex engine as a reference to capturing group 10 rather than group 1 followed by character '0'. C. \g<1>0 compiles to binary C code. D. \10 inserts an octal newline.
\g<1>0 disambiguates that you are referencing group 1 followed by a literal '0', rather than group 10.In the next lesson, we will continue your advanced Python journey with Project: Log File Analyzer (11: Regular Expressions Advanced).
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Greedy vs Non-Greedy Matching | Project: Log File Analyzer |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.