Tutorial by Examples

The first argument of re.match() is the regular expression, the second is the string to match: import re pattern = r"123" string = "123zzb" re.match(pattern, string) # Out: <_sre.SRE_Match object; span=(0, 3), match='123'> match = re.match(pattern, string) ma...
pattern = r"(your base)" sentence = "All your base are belong to us." match = re.search(pattern, sentence) match.group(1) # Out: 'your base' match = re.search(r"(belong.*)", sentence) match.group(1) # Out: 'belong to us.' Searching is done anywhere in the ...
Grouping is done with parentheses. Calling group() returns a string formed of the matching parenthesized subgroups. match.group() # Group without argument returns the entire match found # Out: '123' match.group(0) # Specifying 0 gives the same result as specifying no argument # Out: '123' Arg...
Special characters (like the character class brackets [ and ] below) are not matched literally: match = re.search(r'[b]', 'a[b]c') match.group() # Out: 'b' By escaping the special characters, they can be matched literally: match = re.search(r'\[b\]', 'a[b]c') match.group() # Out: '[b]' T...
Replacements can be made on strings using re.sub. Replacing strings re.sub(r"t[0-9][0-9]", "foo", "my name t13 is t44 what t99 ever t44") # Out: 'my name foo is foo what foo ever foo' Using group references Replacements with a small number of groups can be made a...
re.findall(r"[0-9]{2,3}", "some 1 text 12 is 945 here 4445588899") # Out: ['12', '945', '444', '558', '889'] Note that the r before "[0-9]{2,3}" tells python to interpret the string as-is; as a "raw" string. You could also use re.finditer() which works in...
import re precompiled_pattern = re.compile(r"(\d+)") matches = precompiled_pattern.search("The answer is 41!") matches.group(1) # Out: 41 matches = precompiled_pattern.search("Or was it 42?") matches.group(1) # Out: 42 Compiling a pattern allows it to be r...
If you want to check that a string contains only a certain set of characters, in this case a-z, A-Z and 0-9, you can do so like this, import re def is_allowed(string): characherRegex = re.compile(r'[^a-zA-Z0-9.]') string = characherRegex.search(string) return not bool(string) ...
You can also use regular expressions to split a string. For example, import re data = re.split(r'\s+', 'James 94 Samantha 417 Scarlett 74') print( data ) # Output: ['James', '94', 'Samantha', '417', 'Scarlett', '74']
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. Flags keyword Below an example for re.search but it works for most functions in the re module. m = re.s...
You can use re.finditer to iterate over all matches in a string. This gives you (in comparison to re.findall extra information, such as information about the match location in the string (indexes): import re text = 'You can try to find an ant in this string' pattern = 'an?\w' # find 'an' either w...
Often you want to match an expression only in specific places (leaving them untouched in others, that is). Consider the following sentence: An apple a day keeps the doctor away (I eat an apple everyday). Here the "apple" occurs twice which can be solved with so called backtracking cont...

Page 1 of 1