regex - taoualiw/My-Knowledge-Base GitHub Wiki

Regular Expressions

A regular expression , regex or regexp (sometimes called a rational expression) is a sequence of characters that define a search pattern. Usually this pattern is used by string searching algorithms for "find" or "find and replace" operations on strings, or for input validation.

In Python a regular expression search is typically written as:

match = re.search(pattern, string)

As shown in the following example which searches for the pattern 'word:' followed by a 3 letter word (details below):

pattern = r'word:\w\w\w'
string = 'an example word:cat!!'
match = re.search(pattern, string)
# If-statement after search() tests if it succeeded
if match:
  print 'found', match.group() ## 'found word:cat'
else:
  print 'did not find'

References:

⚠️ **GitHub.com Fallback** ⚠️