regex python pattern code example

Example 1: python re compile

import re
#	Compile a regular expression pattern into a regular expression object, which can be used for matching using its match(), search() and other methods, described below.

prog = re.compile(pattern)
result = prog.match(string)

#	is equivalent to

result = re.match(pattern, string)

Example 2: python regular expression

import re

# The string you want to find a pattern within
test_string = 'Hello greppers!'

# Creating a regular expression pattern
# This is a simple one which finds "Hello"
pattern = re.compile(r'Hello')

# This locates and returns all the occurences of the pattern
# within the test_string
match = pattern.finditer(test_string)

# Outputs all the ocurrences which were returned as 
# as match objects
for match in matches:
  print(match)

Example 3: regular expression syntax python

1. A fixed string -> abc123
2. Arbitrary repetition -> a*b ( "*" means that you can have an arbitrary
								number (possibly 0) of the previous char
3. Repeat character at least once -> a+b # ab, aaaab
4. Repeat character at most once -> a?b # b, ab
5. Repeat a character a fixed number of timers -> a{5} # aaaaa
6. Repeat a pattern a fixed number of times -> (a*b){3} # baabab, ababaaaab
7. Repeat a character or pattern a variable number of times -> a{2,4} # aa, aaa, aaaa
8. Choice of several characters -> [ab]c # ac, bc
9. Arbitrary mixture of several characters -> [ab]*c # c, aac, abbac
10. Ranges of characters -> [A-H][a-z]* # Aasdfalsd, Hb, G 
11. Characters OTHER than particular one -> [^AB] # C, D
12. Choice of several expressions -> Dr|Mr|Ms|Mrs # Dr, Mr, Mrs, Ms
13. Nesting expressions -> ([A-Z][a-z][0-9])* # A, AzSDFcvfg
14. Start of a line -> ^ab
15. End of a line -> ab$

#Type of pattern
1. Special characters -> \[ # [
2. Any charactter 'except' newline -> . # a, *, -
3. Nongreedy evaluation -> <.*>? # <h1></h2 name = "foo">
4. Whitespace -> \s

Example 4: python re.search()

## Search for pattern 'bb' in string 'aabbcc'.
  ## All of the pattern must match, but it may appear anywhere.
  ## On success, match.group() is matched text.
  match = re.search(r'bb', 'aabbcc') # found, match.group() == "bb"
  match = re.search(r'cd', 'aabbcc') # not found, match == None

  ## . = any char but \n
  match = re.search(r'...c', 'aabbcc') # found, match.group() == "abbc"

  ## \d = digit char, \w = word char
  match = re.search(r'\d\d\d', 'p123g') # found, match.group() == "123"
  match = re.search(r'\w\w\w', '@@abcd!!') # found, match.group() == "abc"

Example 5: regex in python

>>> import re
>>> p = re.compile('ab*')
>>> p
re.compile('ab*')