Character sets in regular expressions allow you to match any character from a specified group. For example, the pattern 'con[sc]en[sc]us' will match 'consensus', 'concensus', 'consencus', or 'concencus'.
# Matching 'consensus' or variations import re pattern = re.compile('con[sc]en[sc]us') matches = pattern.findall('concensus consensus consencus concencus') print(matches) # Output: ['concensus', 'consensus', 'consencus', 'concencus']
Optional characters in regular expressions are denoted by a question mark '?'. This means the character can appear once or not at all. For example, 'humou?r' matches both 'humour' and 'humor'.
# Matching 'humor' or 'humour' import re pattern = re.compile('humou?r') matches = pattern.findall('humor humour') print(matches) # Output: ['humor', 'humour']
Literals in regular expressions are used for exact text matches. For instance, the regex 'monkey' will find 'monkey' in text but also match it within a longer sentence like 'The monkeys like bananas'.
# Matching 'monkey' in text import re pattern = re.compile('monkey') matches = pattern.findall('The monkey and the monkeys') print(matches) # Output: ['monkey', 'monkey']
Fixed quantifiers in regular expressions are denoted by curly braces '{}'. They specify a fixed number or range of characters. For example, 'roa{3}r' matches 'roaaaar', and 'roa{3,6}r' matches 'roaaaar' to 'roaaaaaar'.
# Matching 'roaaaar' to 'roaaaaaar' import re pattern = re.compile('roa{3,6}r') matches = pattern.findall('roaaar roaaaar roaaaaar roaaaaaar') print(matches) # Output: ['roaaar', 'roaaaar', 'roaaaaar', 'roaaaaaar']
Alternation in regular expressions, denoted by '|', allows you to match one pattern or another. For instance, 'baboons|gorillas' matches either 'baboons' or 'gorillas'.
# Matching 'baboons' or 'gorillas' import re pattern = re.compile('baboons|gorillas') matches = pattern.findall('baboons gorillas baboons') print(matches) # Output: ['baboons', 'gorillas', 'baboons']
Anchors like '^' and '$' are used to match the start and end of a string. For example, '^Monkeys: my mortal enemy$' matches exactly 'Monkeys: my mortal enemy' and nothing else.
# Matching exact text import re pattern = re.compile('^Monkeys: my mortal enemy$') matches = pattern.findall('Monkeys: my mortal enemy') print(matches) # Output: ['Monkeys: my mortal enemy']
Regular expressions are patterns used to find specific text in strings. They can be used for searching, extracting, or replacing text based on defined patterns.
Wildcards in regular expressions, denoted by '.', match any single character. For example, '......' will match any 6-character string.
# Matching any 6-character string import re pattern = re.compile('......') matches = pattern.findall('orangutan marsupial') print(matches) # Output: ['orangutan', 'marsupial']
Character ranges in regular expressions specify a set of characters. For example, '[A-Z]' matches any uppercase letter, '[0-9]' matches any digit, and '[A-Za-z]' matches any letter.
# Matching uppercase letters and digits import re pattern = re.compile('[A-Z0-9]+') # Modify to match one or more characters matches = pattern.findall('A B 1 2 C D 3') print(matches) # Output: ['A', '1', '2', 'C', 'D', '3']
Shorthand characters in regular expressions simplify common patterns. For example, '\w' matches any word character, '\d' matches digits, and '\W' matches non-word characters.
# Matching word characters and digits import re pattern = re.compile('\w\d') matches = pattern.findall('a1 b2 c3') print(matches) # Output: ['a1', 'b2', 'c3']
The Kleene star '*' in regular expressions means that the preceding character can appear 0 or more times, while the Kleene plus '+' means 1 or more times. For example, 'meo*w' matches 'mew', 'meow', 'meooow', and 'meoooooooooooow', while 'meo+w' matches 'meow', 'meooow', and 'meoooooooooooow', but not 'mew'.
# Matching various repetitions of 'o' import re pattern_star = re.compile('meo*w') pattern_plus = re.compile('meo+w') print(pattern_star.findall('mew meow meooow meoooooooooooow')) # Output: ['mew', 'meow', 'meooow', 'meoooooooooooow'] print(pattern_plus.findall('mew meow meooow meoooooooooooow')) # Output: ['meow', 'meooow', 'meoooooooooooow']
Grouping in regular expressions is done using parentheses. This allows you to apply operations to part of a pattern. For example, 'I love (baboons|gorillas)' matches 'I love baboons' or 'I love gorillas'.
# Matching with grouping import re pattern = re.compile('I love (baboons|gorillas)') matches = pattern.findall('I love baboons I love gorillas') print(matches) # Output: ['baboons', 'gorillas']
Welcome to our comprehensive collection of programming language cheatsheets! Whether you're a seasoned developer or a beginner, these quick reference guides provide essential tips and key information for all major languages. They focus on core concepts, commands, and functions—designed to enhance your efficiency and productivity.
ManageEngine Site24x7, a leading IT monitoring and observability platform, is committed to equipping developers and IT professionals with the tools and insights needed to excel in their fields.
Monitor your IT infrastructure effortlessly with Site24x7 and get comprehensive insights and ensure smooth operations with 24/7 monitoring.
Sign up now!