Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Mastering Python Regular Expressions
Mastering Python Regular Expressions

Mastering Python Regular Expressions: For Python developers, this concise and down-to-earth guide to regular expressions

eBook
$9.99 $16.99
Paperback
$26.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

Mastering Python Regular Expressions

Chapter 1. Introducing Regular Expressions

Regular expressions are text patterns that define the form a text string should have. Using them, among other usages, it will be possible to do the following activities:

  • Check if an input honors a given pattern; for example, we can check whether a value entered in a HTML formulary is a valid e-mail address

  • Look for a pattern appearance in a piece of text; for example, check if either the word "color" or the word "colour" appears in a document with just one scan

  • Extract specific portions of a text; for example, extract the postal code of an address

  • Replace portions of text; for example, change any appearance of "color" or "colour" with "red"

  • Split a larger text into smaller pieces, for example, splitting a text by any appearance of the dot, comma, or newline characters

In this chapter, we are going to learn the basics of regular expressions from a language-agnostic point of view. At the end of the chapter, we will understand how regular expressions work, but we won't yet be able to execute a regular expression in Python. This is going to be covered in the next chapter. Because of this reason, the examples in this chapter will be approached from a theoretical point of view rather than being executed in Python.

History, relevance, and purpose


Regular expressions are pervasive. They can be found in the newest offimatic suite or JavaScript framework to those UNIX tools dating back to the 70s. No modern programming language can be called complete until it supports regular expressions.

Although they are prevalent in languages and frameworks, regular expressions are not yet pervasive in the modern coder's toolkit. One of the reasons often used to explain this is the tough learning curve that they have. Regular expressions can be difficult to master and very complex to read if they are not written with care.

As a result of this complexity, it is not difficult to find in Internet forums the old chestnut:

 

"Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems."

 
 --Jamie Zawinski, 1997

You'll find it at https://groups.google.com/forum/?hl=en#!msg/alt.religion.emacs/DR057Srw5-c/Co-2L2BKn7UJ.

Going through this book, we'll learn how to leverage the best practices when writing regular expressions to greatly simplify the reading process.

Even though regular expressions can be found in the latest and greatest programming languages nowadays and will, probably, for many years on, their history goes back to 1943 when the neurophysiologists Warren McCulloch and Walter Pitts published A logical calculus of the ideas immanent in nervous activity. This paper not only represented the beginning of the regular expressions, but also proposed the first mathematical model of a neural network.

The next step was taken in 1956, this time by a mathematician. Stephen Kleene wrote the paper Representation of events in nerve nets and finite automata, where he coined the terms regular sets and regular expressions.

Twelve years later, in 1968, a legendary pioneer of computer science took Kleene's work and extended it, publishing his studies in the paper Regular Expression Search Algorithm. This engineer was Ken Thompson, known for the design and implementation of Unix, the B programming language, the UTF-8 encoding, and others.

Ken Thompson's work didn't end in just writing a paper. He included support for these regular expressions in his version of QED. To search with a regular expression in QED, the following had to be written:

g/<regular expression>/p

In the preceding line of code, g means global search and p means print. If, instead of writing regular expression, we write the short form re, we get g/re/p, and therefore, the beginnings of the venerable UNIX command-line tool grep.

The next outstanding milestones were the release of the first non-proprietary library of regex by Henry Spence, and later, the creation of the scripting language Perl by Larry Wall. Perl pushed the regular expressions to the mainstream.

The implementation in Perl went forward and added many modifications to the original regular expression syntax, creating the so-called Perl flavor. Many of the later implementations in the rest of the languages or tools are based on the Perl flavor of regular expressions.

The IEEE thought their POSIX standard has tried to standardize and give better Unicode support to the regular expression syntax and behaviors. This is called the POSIX flavor of the regular expressions.

Today, the standard Python module for regular expressions—re—supports only Perl-style regular expressions. There is an effort to write a new regex module with better POSIX style support at https://pypi.python.org/pypi/regex. This new module is intended to replace Python's re module implementation eventually. In this book, we will learn how to leverage only the standard re module.

Tip

Regular expressions, regex, regexp, or regexen?

Henry Spencer referred indistinctly to his famous library as "regex" or "regexp". Wikipedia proposed regex or regexp to be used as abbreviations. The famous Jargon File lists them as regexp, regex, and reg-ex.

However, even though there does not seem to be a very strict approach to naming regular expressions, they are based in the field of mathematics called formal languages, where being exact is everything. Most modern implementations support features that cannot be expressed in formal languages, and therefore, they are not real regular expressions. Larry Wall, creator of the Perl language, used the term regexes or regexen for this reason.

In this book, we will indistinctly use all the aforementioned terms as if they were perfect synonyms.

The regular expression syntax


Any experienced developer has undoubtedly used some kind of regular expression. For instance, in the operating system console, it's not uncommon to find the usage of the asterisk (*) or the question mark (?) to find files.

The question mark will match a single character with any value on a filename. For example, a pattern such as file?.xml will match file1.xml, file2.xml, and file3.xml, but it won't match file99.xml as the pattern expresses that anything that starts with file, followed by just one character of any value, and ends with .xml, will be matched.

A similar meaning is defined for asterisk (*). When asterisk is used, any number of characters with any value is accepted. In the case of file*.xml, anything that starts with file, followed by any number of characters of any value, and finishes with .xml, will be matched.

In this expression, we can find two kind of components: literals (file and .xml) and metacharacters (? or *). The regular expressions we will learn in this book are much more powerful than the simple patterns we can typically find on the operating system command line, but both can share one single definition:

A regular expression is a pattern of text that consists of ordinary characters (for example, letters a through z or numbers 0 through 9) and special characters known as metacharacters. This pattern describes the strings that would match when applied to a text.

Let's see our very first regular expression that will match any word starting with a:

Regex using literals and metacharacters

Note

Representation of regular expressions in this book

In the following figures of this book, regular expressions are going to be represented bounded by the / symbol. This is the QED demarcation that is followed in most of the text books. The code examples, however, won't use this notation.

On the other hand, even with monospaced font faces, the white spaces of a regular expression are difficult to count. In order to simplify the reading, every single whitespace in the figures will appear as .

The previous regular expression is again using literals and metacharacters. The literals here are and a, and the metacharacters are \ and w that match any alphanumeric character including underscore, and *, that will allow any number of repetitions of the previous character, and therefore, any number of repetitions of any word character, including underscore.

We will cover the metacharacters later in this chapter, but let's start by understanding the literals.

Literals

Literals are the simplest form of pattern matching in regular expressions. They will simply succeed whenever that literal is found.

If we apply the regular expression /fox/ to search the phrase The quick brown fox jumps over the lazy dog, we will find one match:

Searching using a regular expression

However, we can also obtain several results instead of just one, if we apply the regular expression /be/ to the following phrase To be, or not to be:

Multiple results searching with regex

We have just learned in the previous section that metacharacters can coexist with literals in the same expression. Because of this coexistence, we can find that some expressions do not mean what we intended. For example, if we apply the expression /(this is inside)/ to search the text this is outside (this is inside), we will find that the parentheses are not included in the result. This happens because parentheses are metacharacters and they have a special meaning.

Incorrectly unescaped metacharacters

We can use metacharacters as if they were literals. There are three mechanisms to do so:

  • Escape the metacharacters by preceding them with a backslash.

  • In python, use the re.escape method to escape non-alphanumeric characters that may appear in the expression. We will cover this in Chapter 2, Regular Expressions with Python.

  • Quoting with \Q and \E: There is a third mechanism to quote in regular expressions, the quoting with \Q and \E. In the flavors that support them, it's as simple as enclosing the parts that have to be quoted with \Q (which starts a quote) and \E (which ends it).

However, this is not supported in Python at the moment.

Using the backslash method, we can convert the previous expression to /\(this is inside\)/ and apply it again to the same text to have the parentheses included in the result:

Escaped metacharacters in regex

In regular expressions, there are twelve metacharacters that should be escaped if they are to be used with their literal meaning:

  • Backslash \

  • Caret ^

  • Dollar sign $

  • Dot .

  • Pipe symbol |

  • Question mark ?

  • Asterisk *

  • Plus sign +

  • Opening parenthesis (

  • Closing parenthesis )

  • Opening square bracket [

  • The opening curly brace {

In some cases, the regular expression engines will do their best to understand if they should have a literal meaning even if they are not escaped; for example, the opening curly brace { will only be treated as a metacharacter if it's followed by a number to indicate a repetition, as we will learn later in this chapter.

Character classes

We are going to use a metacharacter for the first time to learn how to leverage the character classes. The character classes (also known as character sets) allow us to define a character that will match if any of the defined characters on the set is present.

To define a character class, we should use the opening square bracket metacharacter [, then any accepted characters, and finally close with a closing square bracket ]. For instance, let's define a regular expression that can match the word "license" in British and American English written form:

Searching using a character class

It is possible to also use the range of a character. This is done by leveraging the hyphen symbol (-) between two related characters; for example, to match any lowercase letter we can use [a-z]. Likewise, to match any single digit we can define the character set [0-9].

The character classes' ranges can be combined to be able to match a character against many ranges by just putting one range after the other—no special separation is required. For instance, if we want to match any lowercase or uppercase alphanumeric character, we can use [0-9a-zA-Z] (see next table for a more detailed explanation). This can be alternatively written using the union mechanism: [0-9[a-z[A-Z]]].

Element

Description

[

Matches the following set of characters

0-9

Matches anything between 0 and 9 (0, 1, 2, 3, 4, 5, 6, 7, 8, 9).

 

Or

a-z

Matches anything between a and z (a, b, c, d, ..., z)

 

Or

A-Z

Matches anything between A and Z (A, B, C, D, ..., Z)

]

End of character set

There is another possibility—the negation of ranges. We can invert the meaning of a character set by placing a caret (^) symbol right after the opening square bracket metacharacter ([). If we have a character class such as [0-9] meaning any digit, the negated character class [^0-9] will match anything that is not a digit. However, it is important to notice that there has to be a character that is not a digit; for example, /hello[^0-9]/ won't match the string hello because after the there has to be a non-digit character. There is a mechanism to do this—called negative lookahead—and it will be covered in Chapter 4, Look Around.

Predefined character classes

After using character classes for some time, it becomes clear that some of them are very useful and probably worthy of a shortcut.

Luckily enough, there are a number of predefined character classes that can be re-used and will be already known by other developers, making the expressions using them more readable.

These characters are not only useful as well-known shortcuts for typical character sets, but also have different meanings in different contexts. The character class \w, which matches any alphanumeric character, will match a different set of characters depending on the configured locale and the support of Unicode.

The following table shows the character classes supported at this moment in Python:

Element

Description (for regex with default flags)

.

This element matches any character except newline \n

\d

This matches any decimal digit; this is equivalent to the class [0-9]

\D

This matches any non-digit character; this is equivalent to the class [^0-9]

\s

This matches any whitespace character; this is equivalent to the class [\t\n\r\f\v]

\S

This matches any non-whitespace character; this is equivalent to the class [^ \t\n\r\f\v]

\w

This matches any alphanumeric character; this is equivalent to the class [a-zA-Z0-9_]

\W

This matches any non-alphanumeric character; this is equivalent to the class [^a-zA-Z0-9_]

Note

POSIX character classes in Python

The POSIX standard provides a number of character classes' denominations, for example, [:alnum:] for alphanumeric characters, [:alpha:] for alphabetic characters, or [:space:] for all whitespace characters, including line breaks.

All the POSIX character classes follow the same [:name:] notation, rendering them easily identifiable. However, they are not supported in Python at the moment.

If you come across one of them, you can implement the same functionality by leveraging the character classes' functionalities we just studied in this section. As an example, for an ASCII equivalent of [:alnum:] with an English locale, we can write [a-zA-Z0-9].

The first one from the previous table—the dot—requires special attention. The dot is probably one of the oldest and also one of the most used metacharacters. The dot can match any character except a newline character.

The reason to not match the newline is probably UNIX. In UNIX, the command-line tools usually worked line by line, and the regular expressions available at the moment were applied separately to those lines. Therefore, there were no newline characters to match.

Let's put the dot in practice by creating a regular expression that matches three characters of any value except newline:

/…/

Element

Description

.

Matches any character

.

Matches any character followed by the previous one

.

Matches any character followed by the previous one

The dot is a very powerful metacharacter that can create problems if it is not used moderately. In most of the cases where the dot is used, it could be considered overkill (or just a symptom of laziness when writing regular expressions).

To better define what is expected to be matched and to express more concisely to any ulterior reader what a regular expression is intended to do, the usage of character classes is much recommended. For instance, when working with Windows and UNIX file paths, to match any character except the slash or the backslash, you can use a negated character set:

[^\/\]

Element

Description

[

Matches a set of characters

^

Not matching this symbol's following characters

\/

Matches a / character

\

Matches a \ character

]

End of the set

This character set is explicitly telling you that we intend to match anything but a Windows or UNIX file path separator.

Alternation

We have just learned how to match a single character from a set of characters. Now, we are going to learn a broader approach: how to match against a set of regular expressions. This is accomplished using the pipe symbol |.

Let's start by saying that we want to match either if we find the word "yes" or the word "no". Using alternation, it will be as simple as:

/yes|no/

Element

Description

 

Matches either of the following character sets

yes

The characters y, e, and s.

|

Or

no

The characters n and o.

On the other hand, if we want to accept more than two values, we can continue adding values to the alternation like this:

/yes|no|maybe/

Element

Description

 

Matches either of the following character sets

yes

The literal "yes"

|

Or

no

The literal "no"

|

Or

maybe

The literal "maybe"

When using in bigger regular expressions, we will probably need to wrap our alternation inside parentheses to express that only that part is alternated and not the whole expression. For instance, if we make the mistake of not using the parentheses, as in the following expression:

/Licence: yes|no/

Element

Description

 

Matches either of the following character sets

Licence: yes

The characters L, i, c, e, n, c, e, :, , y, e, and s

|

Or

no

The characters n and o.

We may think we are accepting either Licence: yes or Licence: no, but we are actually accepting either Licence: yes or no as the alternation has been applied to the whole regular expression instead of just the yes|no part. A correct approach for this will be:

Regular expression using alternation

Quantifiers

So far, we have learned how to define a single character in a variety of fashions. At this point, we will leverage the quantifiers—the mechanisms to define how a character, metacharacter, or character set can be repeated.

For instance, if we define that a \d can be repeated many times, we can easily create a form validator for the number of items field of a shopping cart (remember that \d matches any decimal digit). But let's start from the beginning, the three basic quantifiers: the question mark ?, the plus sign +, and the asterisk *.

Symbol

Name

Quantification of previous character

?

Question mark

Optional (0 or 1 repetitions)

*

Asterisk

Zero or more times

+

Plus sign

One or more times

{n,m}

Curly braces

Between n and m times

In the preceding table, we can find the three basic quantifiers, each with a specific utility. The question mark can be used to match the word car and its plural form cars:

/cars?/

Element

Description

car

Matches the characters c, a, r and s

s?

Optionally matches the character s

Note

In the previous example, the question mark is only applied to the character s and not to the whole word. The quantifiers are always applied only to the previous token.

Another interesting example of the usage of the question mark quantifier will be to match a telephone number that can be in the format 555-555-555, 555 555 555, or 555555555.

We now know how to leverage character sets to accept different characters, but is it possible to apply a quantifier to a character set? Yes, quantifiers can be applied to characters, character sets, and even to groups (a feature we will cover in Chapter 3, Grouping). We can construct a regular expression like this to validate the telephone numbers:

/\d+[-\s]?\d+[-\s]?\d+/

In the next table, we can find a detailed explanation of the preceding regular expression:

Element

Type

Description

\d

Predefined character set

Any decimal character

+

Quantifier

- that is repeated one or more times

[-\s]

Character set

A hyphen or whitespace character

?

Quantifier

- that may or may not appear

\d

Predefined character set

Any decimal character

+

Quantifier

- that is repeated one or more times

[-\s]

Character set

A hyphen or whitespace character

\d

Predefined character set

Any decimal character

+

Quantifier

- that is repeated one or more times

At the beginning of this section, one more kind of quantifier using the curly braces had been mentioned. Using this syntax, we can define that the previous character must appear exactly three times by appending it with {3}, that is, the expression \w{8} specifies exactly eight alphanumeric digits.

We can also define a certain range of repetitions by providing a minimum and maximum number of repetitions, that is, between three and eight times can be defined with the syntax {4,7}. Either the minimum or the maximum value can be omitted defaulting to 0 and infinite respectively. To designate a repetition of up to three times, we can use {,3}, we can also establish a repetition at least three times with {3,}.

Tip

Readability Tip

Instead of using {,1}, you can use the question mark. The same applies to {0,} for the asterisk * and {1,} for the plus sign +.

Other developers will expect you to do so. If you don't follow this practice, anyone reading your expressions will lose some time trying to figure out what kind of fancy stuff you tried to accomplish.

These four different combinations are shown in the next table:

Syntax

Description

{n}

The previous character is repeated exactly n times.

{n,}

The previous character is repeated at least n times.

{,n}

The previous character is repeated at most n times.

{n,m}

The previous character is repeated between n and m times (both inclusive).

Earlier in this chapter, we created a regular expression to validate telephone numbers that can be in the format 555-555-555, 555 555 555, or 555555555. We defined a regular expression to validate it using the metacharacter plus sign: /\d+[-\s]?\d+[-\s]?\d+/. It will require the digits (\d) to be repeated one or more times.

Let's fine-tune the regular expression by defining that the leftmost digit group can contain up to three characters, while the rest of the digit groups should contain exactly three digits:

Using quantifiers

Greedy and reluctant quantifiers

We still haven't defined what would match if we apply a quantifier such as this /".+"/ to a text such as the following: English "Hello", Spanish "Hola". We may expect that it matches "Hello" and "Hola" but it will actually match "Hello", Spanish "Hola".

This behavior is called greedy and is one of the two possible behaviors of the quantifiers in Python: greedy and non-greedy (also known as reluctant).

  • The greedy behavior of the quantifiers is applied by default in the quantifiers. A greedy quantifier will try to match as much as possible to have the biggest match result possible.

  • The non-greedy behavior can be requested by adding an extra question mark to the quantifier; for example, ??, *? or +?. A quantifier marked as reluctant will behave like the exact opposite of the greedy ones. They will try to have the smallest match possible.

Note

Possessive quantifiers

There is a third behavior of the quantifiers, the possessive behavior. This behavior is only supported by the Java and .NET flavors of the regular expressions at the moment.

They are represented with an extra plus symbol to the quantifier; for example, ?+, *+, or ++. Possessive quantifiers won't have further coverage in this book.

We can understand better how this quantifier works by looking at the next figure. We will apply almost the same regular expression (with the exception of leaving the quantifier as greedy or marking it as reluctant) to the same text, having two very different results:

Greedy and reluctant quantifiers

Boundary Matchers

Until this point, we have just tried to find out regular expressions within a text. Sometimes, when it is required to match a whole line, we may also need to match at the beginning of a line or even at the end. This can be done thanks to the boundary matchers.

The boundary matchers are a number of identifiers that will correspond to a particular position inside of the input. The following table shows the boundary matchers available in Python:

Matcher

Description

^

Matches at the beginning of a line

$

Matches at the end of a line

\b

Matches a word boundary

\B

Matches the opposite of \b. Anything that is not a word boundary

\A

Matches the beginning of the input

\Z

Matches the end of the input

These boundary matchers will behave differently in different contexts. For instance, the word boundaries (\b) will depend directly on the configured locale as different languages may have different word boundaries, and the beginning and end of line boundaries will behave differently based on certain flags that we will study in the next chapter.

Let's start working with boundary matchers by writing a regular expression that will match lines that start with "Name:". If you take a look at the previous table, you may notice the existence of the metacharacter ^ that expresses the beginning of a line. Using it, we can write the following expression:

/^Name:/

Element

Description

^

Matches the beginning of the line

N

Matches the followed by character N

a

Matches the followed by character a

m

Matches the followed by character m

e

Matches the followed by character e

:

Matches the followed by symbol colon

If we want to take one step further and continue using the caret and the dollar sign in combination to match the end of the line, we should take into consideration that from now on we are going to be matching against the whole line, and not just trying to find a pattern within a line.

Following the previous example, let's say that we want to make sure that after the name, there are only alphabetic characters or spaces until the end of the line. We will do this by matching the whole line until the end by setting a character set with the accepted characters and allowing their repetition any number of times until the end of the line.

/^Name:[\sa-zA-Z]+$/

Element

Description

^

Matches the beginning of the line.

N

Matches the followed by character N.

a

Matches the followed by character a.

m

Matches the followed by character m.

e

Matches the followed by character e.

:

Matches the followed by colon symbol.

[\sa-zA-Z]

Then matches the followed by whitespace, or any alphabetic lowercase or uppercase character.

+

The character can be repeated one or more times.

$

Until the end of the line.

Another outstanding boundary matcher is the word boundary \b. It will match any character that is not a word character (in the configured locale), and therefore, any potential word boundary. This is very useful when we want to work with isolated words and we don't want to create character sets with every single character that may divide our words (spaces, commas, colons, hyphens, and so on). We can, for instance, make sure that the word hello appears in a text by using the following regular expression:

/\bhello\b/

Element

Description

\b

Matches a word boundary.

h

Matches the followed by character h.

e

Matches the followed by character e.

l

Matches the followed by character l.

l

Matches the followed by character l.

o

Matches the followed by character o.

\b

Then matches another followed by word boundary.

As an exercise, we could think why the preceding expression is better than /hello/. The reason is that this expression will match an isolated word instead of a word containing "hello", that is, /hello/ will easily match hello, helloed, or Othello; while /\bhello\b/ will only match hello.

Summary


In this first chapter, we have learned the importance of the regular expressions and how they became such a relevant tool for the programmers.

We also studied from a yet non-practical point of view, the basic regular expression syntax and some of the key features, such as character classes and quantifiers.

In the next chapter, we are going to jump over to Python to start practicing with the re module.

Left arrow icon Right arrow icon

Description

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.

Who is this book 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.

What you will learn

  • 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
Estimated delivery fee Deliver to Ecuador

Standard delivery 10 - 13 business days

$19.95

Premium delivery 3 - 6 business days

$40.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Feb 21, 2014
Length: 110 pages
Edition :
Language : English
ISBN-13 : 9781783283156
Category :
Languages :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Ecuador

Standard delivery 10 - 13 business days

$19.95

Premium delivery 3 - 6 business days

$40.95
(Includes tracking information)

Product Details

Publication date : Feb 21, 2014
Length: 110 pages
Edition :
Language : English
ISBN-13 : 9781783283156
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 75.98
Mastering Object-oriented Python
$48.99
Mastering Python Regular Expressions
$26.99
Total $ 75.98 Stars icon
Banner background image

Table of Contents

5 Chapters
Introducing Regular Expressions Chevron down icon Chevron up icon
Regular Expressions with Python Chevron down icon Chevron up icon
Grouping Chevron down icon Chevron up icon
Look Around Chevron down icon Chevron up icon
Performance of Regular Expressions Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.7
(10 Ratings)
5 star 70%
4 star 30%
3 star 0%
2 star 0%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




New Media Aug 06, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Great Learning tool.
Amazon Verified review Amazon
Javier Angulo Lucerón Jun 16, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
It's a really well written book, with tons of examples to allow you understand from the basics to the most advanced features. I'm starting to leverage regular expressions thanks to this book, quite recommended.
Amazon Verified review Amazon
Doc Jun 19, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I used this book to learn and master regular expressions in Python. It is easy to follow and if you study each chapter carefully you will master regular expressions. It starts out with a brief overview of regex and then transitions to specific application of regex in python. Also great to have as a desk reference.
Amazon Verified review Amazon
Uncle Buck Sep 24, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
If you are getting started with regular expressions the first chapter of this book is worth the price of the book in itself.I cannot praise this book enough. The well written and clear examples are fantastic. I've been struggling with regular expressions for a while but this book is a god send. Thank you Felix Lopez!!!!!
Amazon Verified review Amazon
DR Jan 05, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
A great, concise and thorough book on using Regex in Python. Excellent, thoroughly recommended.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela