How to Create a Regular Expression in Python

Searching for patterns in text can come in handy in a wide variety of tasks. With regular expressions, you can harness Python to make otherwise time-consuming and tedious data-massaging tasks simple to complete. Python supports extended regular expressions, meaning that you can get a comparable functionality to perl or the php PCRE regular expression engines.

Instructions

  1. Compile the Expression

    • 1

      Import the regular expression module into your Python code by adding "import re" at the top of your code.

    • 2

      Create a regular expression object by using the re.compile method:
      myregex = re.compile("my\wexample\wregex")

    • 3

      Pass an optional second argument to re.compile that indicates modifiers to the regular expression. For example, if you want to make your regular expression match over multiple lines and be case insensitive, you can compile the expression like:
      myregex = re.compile('my\wexample\wregex', 're.IGNORECASE | re.MULTILINE')

    Search

    • 4

      Create a variable that stores the results of your compiled regular expression. Use your compiled regular expression to search on a string and assign the results:
      results = myregex.search('use my example regex to search this string')

    • 5

      Use your match object variable and use the start() function to find the start of the first occurrence of the regular expression:
      print results.start()
      >> 3

    • 6

      Use the end() function to find the end of the first occurrence of the regular expression:
      print results.end()
      >> 21

    • 7

      Use the group() function to find the string that matched your regular expression:
      print results.group()
      >>> my example regex

    Replace

    • 8

      Use the regular expression to perform a substitution in your string using the re.sub routine, if necessary.

    • 9

      Create a new string variable to hold the results of your substitution. The first argument is the substitution text, and the second argument is the string to be matched on:
      newstring = myregex.sub('kung fu', 'use my example regex to regex this string')
      print newstring
      >>> use my kung fu to regex this string

Tips & Warnings

  • The Python Regular Expression How To contains a solid introduction on the process of working with regular expressions in any language, as well as full documentation on all major regex routines and modifiers (see Resources below).

  • Python regular expressions do not directly match on Unicode tokens. However, you can use the \uFFFF multi-byte notation to perform Unicode matches.

Related Searches:

Resources

Comments

You May Also Like

Related Ads

Featured