Mastering Python Regular Expressions

Mastering Python Regular Expressions
eBook: $14.99
Formats: PDF, PacktLib, ePub and Mobi formats
save 15%!
Print + free eBook + free PacktLib access to the book: $39.98    Print cover: $24.99
save 37%!
Free Shipping!
UK, US, Europe and selected countries in Asia.
Also available on:
Table of Contents
Sample Chapters
  • Explore the workings of Regular Expressions in Python
  • Learn all about optimizing regular expressions using RegexBuddy
  • Full of practical and step-by-step examples, tips for performance, and solutions for performance-related problems faced by users all over the world

Book Details

Language : English
Paperback : 110 pages [ 235mm x 191mm ]
Release Date : February 2014
ISBN : 1783283157
ISBN 13 : 9781783283156
Author(s) : Félix López, Víctor Romero
Topics and Technologies : All Books, Application Development, Open Source

Table of Contents

Chapter 1: Introducing Regular Expressions
Chapter 2: Regular Expressions with Python
Chapter 3: Grouping
Chapter 4: Look Around
Chapter 5: Performance of Regular Expressions
  • Chapter 1: Introducing Regular Expressions
    • History, relevance, and purpose
    • The regular expression syntax
      • Literals
      • Character classes
      • Predefined character classes
      • Alternation
      • Quantifiers
        • Greedy and reluctant quantifiers
      • Boundary Matchers
    • Summary
  • Chapter 2: Regular Expressions with Python
    • A brief introduction
    • Backslash in string literals
      • String Python 2.x
    • Building blocks for Python regex
      • RegexObject
        • Searching
        • Modifying a string
      • MatchObject
        • group([group1, …])
        • groups([default])
        • groupdict([default])
        • start([group])
        • end([group])
        • span([group])
        • expand(template)
      • Module operations
        • escape()
        • purge()
    • Compilation flags
      • re.IGNORECASE or re.I
      • re.MULTILINE or re.M
      • re.DOTALL or re.S
      • re.LOCALE or re.L
      • re.UNICODE or re.U
      • re.VERBOSE or re.X
      • re.DEBUG
    • Python and regex special considerations
      • Differences between Python and other flavors
      • Unicode
      • What's new in Python 3
    • Summary
  • Chapter 3: Grouping
    • Introduction
    • Backreferences
    • Named groups
    • Non-capturing groups
      • Atomic groups
    • Special cases with groups
      • Flags per group
      • yes-pattern|no-pattern
    • Overlapping groups
    • Summary
  • Chapter 4: Look Around
    • Look ahead
      • Negative look ahead
    • Look around and substitutions
    • Look behind
      • Negative look behind
    • Look around and groups
    • Summary
  • Chapter 5: Performance of Regular Expressions
    • Benchmarking regular expressions with Python
    • The RegexBuddy tool
    • Understanding the Python regex engine
      • Backtracking
    • Optimization recommendations
      • Reuse compiled patterns
      • Extract common parts in alternation
      • Shortcut to alternation
      • Use non-capturing groups when appropriate
      • Be specific
      • Don't be greedy
    • Summary

Félix López

Félix López started his career in web development before moving to software in the currency exchange market, where there were a lot of new security challenges. Later, he spent four years creating an IDE to develop games for hundreds of different mobile device OS variations, in addition to creating more than 50 games. Before joining ShuttleCloud, he spent two years working on applications with sensor networks, Arduino, ZigBee, and custom hardware. One example is an application that detects the need for streetlight utilities in major cities based on existing atmospheric brightness. His first experience with Python was seven years ago, He used it for small scripts, web scrapping, and so on. Since then, he has used Python for almost all his projects: websites, standalone applications, and so on. Nowadays, he uses Python along with RabbitMQ in order to integrate services.

He's currently working for ShuttleCloud, an U.S.-based startup, whose technology is used by institutions such as Stanford and Harvard, and companies such as Google.

Víctor Romero

Víctor Romero currently works as a solutions architect at MuleSoft, Inc. He started his career in the dotcom era and has been a regular contributor to open source software ever since. Originally from the sunny city of Malaga, Spain, his international achievements include integrating the applications present in the cloud storage of a skyscraper in New York City, and creating networks for the Italian government in Rome.

Sorry, we don't have any reviews for this title yet.

Code Downloads

Download the code and support files for this book.

Submit Errata

Please let us know if you have found any errors not listed on this list by completing our errata submission form. Our editors will check them and add them to this list. Thank you.


- 1 submitted: last submission 02 Jun 2014

Errata type: Technical | Page number: 14




should be:



Sample chapters

You can view our sample chapters and prefaces of this title on PacktLib or download sample chapters in PDF format.

Frequently bought together

Mastering Python Regular Expressions +    Bootstrap Site Blueprints =
50% Off
the second eBook
Price for both: £19.45

Buy both these recommended eBooks together and get 50% off the cheapest eBook.

What you will learn from this book

  • Explore the regular expressions syntax
  • Improve the readability and future maintenance of the regex
  • Find solutions for typical problems with regular expressions
  • Familiarize yourself with match and search operations
  • Leverage the look around technique to create powerful regular expressions
  • Gain insight on the uses of Groups
  • Get to know how the regex engine works through the Backtracking process
  • Enhance the performance of your regular expressions

In Detail

Regular expressions are used by many text editors, utilities, and programming languages to search and manipulate text based on patterns. They are considered the Swiss army knife of text processing. Powerful search, replacement, extraction and validation of strings, repetitive and complex tasks are reduced to a simple pattern using regular expressions.

Mastering Python Regular Expressions will teach you about Regular Expressions, starting from the basics, irrespective of the language being used, and then it will show you how to use them in Python. You will learn the finer details of what Python supports and how to do it, and the differences between Python 2.x and Python 3.x.

The book starts with a general review of the theory behind the regular expressions to follow with an overview of the Python regex module implementation, and then moves on to advanced topics like grouping, looking around, and performance.

You will explore how to leverage Regular Expressions in Python, some advanced aspects of Regular Expressions and also how to measure and improve their performance. You will get a better understanding of the working of alternators and quantifiers. Also, you will comprehend the importance of grouping before finally moving on to performance optimization techniques like the RegexBuddy Tool and Backtracking.

Mastering Python Regular Expressions provides all the information essential for a better understanding of Regular Expressions in Python.


A short and straight to the point guide that explains the implementation of Regular Expressions in Python.

Who this book is for

This book is aimed at Python developers who want to learn how to leverage Regular Expressions in Python. Basic knowledge of Python is required for a better understanding.

Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software