Hour 16. Regular Expressions


What You’ll Learn in This Hour:

Image What regular expressions are

Image Defining regular expression patterns

Image How to use regular expressions in your scripts


One of the most common functions used in Python scripts is manipulation of string data. One of the things Python is known for is its ability to easily search and modify strings. One of the features in Python that provides support for string parsing is regular expressions. In this hour, you’ll see what regular expressions are, how to use them in Python, and how to leverage them in your own Python scripts.

What Are Regular Expressions?

Many people have a hard time understanding what regular expressions are. The first step to understanding them is defining exactly what they are and what they can do for you. The following sections explain what a regular expression is and describe how Python uses regular expressions to help with your string manipulations.

Definition of Regular Expressions

A regular expression is a pattern you create to filter text. A program or script matches the regular expression pattern you create against data as the data flows through the program. If the data matches the pattern, it’s accepted for processing. If the data doesn’t match the pattern, it’s rejected. Figure 16.1 shows how it works.

Image

FIGURE 16.1 Matching data against a regular expression.

While are probably familiar with normal text searching, regular expressions provides a lot more than that. The regular expression pattern makes use of wildcard characters to represent one or more characters in the data stream. You can use a number of special characters in a regular expression to define a specific pattern for filtering data. This means you have a lot of flexibility in how you define your string patterns.

Types of Regular Expressions

The biggest problem with using regular expressions is that there isn’t just one set of them. Different applications use different types of regular expressions. These include such diverse things as programming languages (for example, Java, Perl, Python), Linux utilities (such as the sed editor, the gawk program, and the grep utility), and mainstream applications (such as the MySQL and PostgreSQL database servers).

A regular expression is implemented using a regular expression engine. A regular expression engine is the underlying software that interprets regular expression patterns and uses those patterns to match text.

In the open source software world, there are two popular regular expression engines:

Image The POSIX Basic Regular Expression (BRE) engine

Image The POSIX Extended Regular Expression (ERE) engine

Most open source programs at a minimum conform to the POSIX BRE engine specifications, recognizing all the pattern symbols it defines. Unfortunately, some utilities (such as the sed editor) only conform to a subset of the BRE engine specifications. This is due to speed constraints, as the sed editor attempts to process text in the data stream as quickly as possible.

The POSIX ERE engine is often found in programming languages that rely on regular expressions for text filtering. It provides advanced pattern symbols as well as special symbols for common patterns, such as matching digits, words, and alphanumeric characters. The Python programming language uses the ERE engine to process its regular expression patterns.

Working with Regular Expressions in Python

Before you can start writing regular expressions to filter data in your Python scripts, you need to know how to use them. The Python language provides the re module to support regular expressions. The re module is included in the Raspbian Python default installation, so you don’t need to do anything special to start using regular expressions in your scripts, other than import the re module at the start of a script:

import re

However, the re module provides two different ways to define and use regular expressions. The following sections discuss how to use both methods.

Regular Expression Functions

The easiest way to use regular expressions in Python is to directly use the regular expression functions provided by the re module. Table 16.1 lists the functions that are available.

Image

TABLE 16.1 The re Module Functions

The re module functions take two parameters. The first parameter is the regular expression pattern, and the second parameter is the text string to apply the pattern to.

The match() and search() regular expression functions return either a True Boolean value if the text string matches the regular expression pattern or a False value if they don’t match. This makes them ideal for use in if-then statements.

The match() Function

The match() function does what it says: It tries to match the regular expression pattern to a text string. It is a little tricky in that it applies the regular expression string only to the start of the string value. Here’s an example:

>>> re.match('test', 'testing')
<_sre.SRE_Match object at 0x015F9950>
>>> re.match('ing', 'testing')
>>>

The output from the first match indicates that the match was successful. When the match fails, the match() function just returns a False value, which doesn’t show any output in the IDLE interface.

The search() Function

The search() function is a lot more versatile than match(): It applies the regular expression pattern to an entire string and returns a True value if it finds the pattern anywhere in the string. Here’s an example:

>>> re.search('ing', 'testing')
<_sre.SRE_Match object at 0x015F9918>
>>>

This output from the search() function indicates that it found the pattern inside the string.

The findall() and finditer() Functions

Both the findall() and finditer() functions returns multiple instances of the pattern if it is found in the string. The findall() function returns a list of values that match in the string, as you can see here:

>>> re.findall('[ch]at', 'The cat wore a hat')
['cat', 'hat']
>>>

The finditer() function returns an iterable object that you can use in a for statement to iterate through the results.

Compiled Regular Expressions

If you find yourself using the same regular expression often in your code, you can compile the expression and store it in a variable. You can then use the variable everywhere in your code that you want to perform the regular expression pattern match.

To compile an expression, you use the compile() function, specifying the regular expression as the parameter and storing the result in a variable, like this:

>>> pattern = re.compile('[ch]at')

After you store the compiled regular expression, you can use it directly in a match() or search() function, as shown here:

>>> pattern.search('This is a cat')
<_sre.SRE_Match object at 0x015F9988>
>>> pattern.search('He wore a hat')
<_sre.SRE_Match object at 0x015F9918>
>>> pattern.search('He sat in a chair')
>>>

The additional benefit of using compiled regular expression patterns is that you can also specify flags to control special features of the regular expression match. Table 16.2 shows these flags and what they control.

Image

TABLE 16.2 Compiled Flags

For example, by default, regular expression matches are case sensitive. To make a check case insensitive, you just compile the regular expression with the re.I flag, as shown here:

>>> pattern = re.compile('[ch]at', re.I)
>>> pattern.search('Cat')
<_sre.SRE_Match object at 0x015F9988>
>>> pattern.search('Hat')
<_sre.SRE_Match object at 0x015F9918>
>>>

The search() function can now match the text in either uppercase or lowercase, anywhere in the text.

Defining Basic Patterns

Defining regular expression patterns falls somewhere between a science and an art form. Entire books have been written about how to create regular expressions for matching different types of data (such as email addresses, phone numbers, or Social Security numbers). Instead of just showing a list of different regular expression patterns, the purpose of this section is to provide the basics of how to use them in daily text searches.

Plain Text

The simplest pattern for searching for text is to use the text that you want to find in its entirety, as in this example:

>>> re.search('test', 'This is a test')
<_sre.SRE_Match object at 0x015F99C0>
>>> re.search('test', 'This is not going to work')
>>>

With the search() function, the regular expression doesn’t care where in the data the pattern occurs. It also doesn’t matter how many times the pattern occurs. When the regular expression can match the pattern anywhere in the text string, it returns a True value.

The key is matching the regular expression pattern to the text. It’s important to remember that regular expressions are extremely picky about matching patterns. Remember that, by default, regular expression patterns are case sensitive. This means they’ll match patterns only for the proper case of characters, as shown here:

>>> re.search('this', 'This is a test')
>>>
>>> re.search('This', 'This is a test')
<_sre.SRE_Match object at 0x015F9988>
>>>

The first attempt here fails to match because the word this doesn’t appear in all lowercase in the text string; the second attempt, using the uppercase letter in the pattern, works just fine.

You don’t have to limit yourself to whole words in a regular expression. If the defined text appears anywhere in the data stream, the regular expression will match, as shown here:

>>> re.search('book', 'The books are expensive')
<_sre.SRE_Match object at 0x015F99C0>
>>>

Even though the text in the data stream is books, the data in the stream contains the regular expression book, so the regular expression pattern matches the data. Of course, if you try the opposite, the regular expression fails, as shown in this example:

>>> re.search('books', 'The book is expensive')
>>>

You also don’t have to limit yourself to single text words in a regular expression. You can include spaces and numbers in your text string as well, as shown here:

>>> re.search('This is line number 1', 'This is line number 1')
<_sre.SRE_Match object at 0x015F9988>
>>> re.search('ber 1', 'This is line number 1')
<_sre.SRE_Match object at 0x015F99F8>
>>> re.search('ber 1', 'This is line number1')
>>>

If you define a space in a regular expression, it must appear in the data stream. You can even create a regular expression pattern that matches multiple contiguous spaces, like this:

>>> re.search('  ', 'This line has  too many spaces')
<_sre.SRE_Match object at 0x015F9988>
>>>

The line with two spaces between words matches the regular expression pattern. This is a great way to catch spacing problems in text files!

Special Characters

As you use text strings in your regular expression patterns, there’s something you need to be aware of: There are a few exceptions when defining text characters in a regular expression. Regular expression patterns assign a special meaning to a few characters. If you try to use these characters in your text pattern, you won’t get the results you were expecting.

Regular expressions recognize these special characters:

. * [ ] ^ $ { } + ? | ( )

As you work your way through this hour, you’ll find out what these special characters do in a regular expression. For now, though, just remember that you can’t use these characters by themselves in your text pattern.

If you want to use one of the special characters as a text character, you need to escape it. To escape a special character, you add another character in front of it to indicate to the regular expression engine to interpret the next character as a normal text character. The special character that does this is the backslash characters ().

In Python, as you’ve learned, backslashes also have special meaning in string values. To get around this, if you want to use the backslash character with a special character, you can create a raw string value, using the r nomenclature:

r'textstring'

For example, if you want to search for a dollar sign in your text, just precede it with a backslash character, like this:

>>> re.search(r'$', 'The cost is $4.00')
<_sre.SRE_Match object at 0x015F9918>
>>>

You can use raw text strings for your regular expressions, even if they don’t contain any backslashes. Some coders just get in the habit of always using the raw text strings.

Anchor Characters

As shown in the “Plain Text” section a little earlier this hour, by default when you specify a regular expression pattern, the pattern can appear anywhere in the data stream and be a match. There are two special characters you can use to anchor a pattern to either the beginning or the end of lines in the data stream: ^ and $.

Starting at the Beginning

The caret character (^) defines a pattern that starts at the beginning of a line of text in the data stream. If the pattern is located anyplace other than the start of the line of text, the regular expression pattern fails.

To use the caret character, you must place it before the pattern specified in the regular expression, like this:

>>> re.search('^book', 'The book store')
>>> re.search('^Book', 'Books are great')
<_sre.SRE_Match object at 0x015F9988>
>>>

The caret anchor character checks for the pattern at the beginning of each string, not each line. If you need to match the beginning of each line of text, you need to use the MULTILINE feature of the compiled regular expression, as in this example:

>>> re.search('^test', 'This is a test of a new line')
>>>
>>> pattern = re.compile('^test', re.MULTILINE)
>>> pattern.search('This is a test of a new line')
<_sre.SRE_Match object at 0x015F9988>
>>>

In the first example, the pattern doesn’t match the word test at the start of the second line in the text. In the second example, using the MULTILINE feature, it does.


By the Way: Caret Versus match()

You’ll notice that the caret special character does the same thing as the match() function. They’re interchangeable when you’re working with scripts.


Looking for the Ending

The opposite of looking for a pattern at the start of a line is looking for a pattern at the end of a line. The dollar sign ($) special character defines the end anchor. You can add this special character after a text pattern to indicate that the line of data must end with the text pattern, as in this example:

>>> re.search('book$', 'This is a good book')
<_sre.SRE_Match object at 0x015F99F8>
>>> re.search('book$', 'This book is good')
>>>

The problem with an ending text pattern is that you must be careful of what you’re looking for, as shown here:

>>> re.search('book$', 'There are a lot of good books')
>>>

Because the book word is plural at the end of the line, it no longer matches the regular expression pattern, even though book is in the data stream. The text pattern must be the very last thing on the line in order for the pattern to match.

Combining Anchors

There are a couple common situations when you can combine the start and end anchors on the same line. In the first situation, suppose you want to look for a line of data that contains only a specific text pattern, as in this example:

>>> re.search('^this is a test$', 'this is a test')
<_sre.SRE_Match object at 0x015F9918>
>>> re.search('^this is a test$', 'I said this is a test')
>>>

The second situation may seem a little odd at first, but it is extremely useful. By combining both anchors together in a pattern with no text, you can filter empty strings. Look at this example:

>>> re.search('^$', 'This is a test string')
>>> re.search('^$', "")
<_sre.SRE_Match object at 0x015F99F8>
>>>

The defined regular expression pattern looks for text that has nothing between the start and end of the line. Because blank lines contain no text between the two newline characters, they match the regular expression pattern. This is an effective way to remove blank lines from documents.

The Dot Character

The dot special character is used to match any single character except a newline character. The dot character must match some character, though; if there’s no character in the place of the dot, the pattern will fail.

Let’s take a look at a few examples of using the dot character in a regular expression pattern:

>>> re.search('.at', 'The cat is sleeping')
<_sre.SRE_Match object at 0x015F9988>
>>> re.search('.at', 'That is heavy')
<_sre.SRE_Match object at 0x015F99F8>
>>> re.search('.at', 'He is at the store')
<_sre.SRE_Match object at 0x015F9988>
>>> re.search('.at', 'at the top of the hour')
>>>

The third test here is a little tricky. Notice that you match the at, but there’s no character in front to match the dot character. Ah, but there is! In regular expressions, spaces count as characters, so the space in front of the at matches the pattern. The last test proves this by putting the at in the front of the line and failing to match the pattern.

Character Classes

The dot special character is great for matching a character position against any character, but what if you want to limit what characters to match? This is called a character class in regular expressions.

You can define a class of characters that would match a position in a text pattern. If one of the characters from the character class is in the data stream, it matches the pattern.

To define a character class, you use square brackets. The brackets contain any character you want to include in the class. You then use the entire class within a pattern, just as you would any other wildcard character. This takes a little getting used to, but once you catch on, you see that you can use it to create some pretty amazing results.

Here’s an example of creating a character class:

>>> re.search('[ch]at', 'The cat is sleeping')
<_sre.SRE_Match object at 0x015F9918>
>>> re.search('[ch]at', 'That is a very nice hat')
<_sre.SRE_Match object at 0x015F99F8>
>>> re.search('[ch]at', 'He is at the store')
>>>

This time, the regular expression pattern matches only strings that have a c or h in front of the at pattern.

You can use more than one character class in a single expression, as in these examples:

>>> re.search('[Yy][Ee][Ss]', 'Yes')
<_sre.SRE_Match object at 0x015F9988>
>>> re.search('[Yy][Ee][Ss]', 'yEs')
<_sre.SRE_Match object at 0x015F99F8>
>>> re.search('[Yy][Ee][Ss]', 'yeS')
<_sre.SRE_Match object at 0x015F9988>
>>>

The regular expression uses three character classes to cover both lowercase and uppercase for all three character positions.

Character classes don’t have to be just letters. You can use numbers in them as well, as shown here:

>>> re.search('[012]', 'This has 1 number')
<_sre.SRE_Match object at 0x015F99F8>
>>> re.search('[012]', 'This has the number 2')
<_sre.SRE_Match object at 0x015F9988>
>>> re.search('[012]', 'This has the number 4')
>>>

The regular expression pattern matches any lines that contain the numbers 0, 1, or 2. Any other numbers are ignored, as are lines without numbers in them.

This is a great feature for checking for properly formatted numbers, such as phone numbers and zip codes. However, remember that the regular expression pattern can be found anywhere in the text of the data stream. There might be additional characters besides the matching pattern characters.

For example, if you want to match against a five-digit zip code, you can ensure that you only match against five numbers by using the start- and end-of-the-line characters:

>>> re.search('^[0123456789][0123456789][0123456789][0123456789][0123456789]$'
, '12345')
<_sre.SRE_Match object at 0x0154FC28>
>>> re.search('^[0123456789][0123456789][0123456789][0123456789][0123456789]$'
, '123456')
>>> re.search('^[0123456789][0123456789][0123456789][0123456789][0123456789]$', '1234')
>>>

If there are fewer than five or more than five numbers in a zip code, the regular expression pattern returns False.

Negating Character Classes

In regular expression patterns, you can reverse the effect of a character class. Instead of looking for a character contained in a class, you can look for any character that’s not in the class. To do this, you place a caret character at the beginning of the character class range, as shown here:

>>> re.search('[^ch]at', 'The cat is sleeping')
>>> re.search('[^ch]at', 'He is at home')
<_sre.SRE_Match object at 0x015F9988>
>>> re.search('[^ch]at', 'at the top of the hour')
>>>

By negating the character class, the regular expression pattern matches any character that’s neither a c nor an h, along with the text pattern. Because the space character fits this category, it passes the pattern match. However, even with the negation, the character class must still match a character, so the line with the at in the start of the line still doesn’t match the pattern.

Using Ranges

You may have noticed in the zip code example that it is rather awkward having to list all the possible digits in each character class. Fortunately, you can use a shortcut to avoid having to do that.

You can use a range of characters within a character class by using the dash symbol. You just specify the first character in the range, a dash, and then the last character in the range. The regular expression includes any character that’s within the specified character range, depending on the character set you defined when you set up your Raspberry Pi system.

Now you can simplify the zip code example by specifying a range of digits:

>>> re.search('^[0-9][0-9][0-9][0-9][0-9]$', '12345')
<_sre.SRE_Match object at 0x01570C98>
>>> re.search('^[0-9][0-9][0-9][0-9][0-9]$', '1234')
>>> re.search('^[0-9][0-9][0-9][0-9][0-9]$', '123456')
>>>

This saves a lot of typing! Each character class matches any digit from 0 to 9. The same technique also works with letters:

>>> re.search('[c-h]at', 'The cat is sleeping')
<_sre.SRE_Match object at 0x0154FC28>
>>> re.search('[c-h]at', "I'm getting too fat")
<_sre.SRE_Match object at 0x01570C98>
>>> re.search('[c-h]at', 'He hit the ball with the bat')
>>>

The new pattern, [c-h]at, only matches words where the first letter is between the letter c and the letter h. In this case, the line with only the word at fails to match the pattern.

You can also specify multiple noncontinuous ranges in a single character class:

>>> re.search('[a-ch-m]at', 'The cat is sleeping')
<_sre.SRE_Match object at 0x0154FC28>
>>> re.search('[a-ch-m]at', 'He hit the ball with the bat')
<_sre.SRE_Match object at 0x01570CD0>
>>> re.search('[a-ch-m]at', "I'm getting too fat")
>>>

The character class allows the ranges a through c and h through m to appear before the at text. This range rejects any letters between d and g.

The Asterisk

Placing an asterisk after a character signifies that the character may appear zero or more times in the text to match the pattern, as shown in this example:

>>> re.search('ie*k', 'ik')
<_sre.SRE_Match object at 0x0154FC28>
>>> re.search('ie*k', 'iek')
<_sre.SRE_Match object at 0x01570CD0>
>>> re.search('ie*k', 'ieek')
<_sre.SRE_Match object at 0x0154FC28>
>>> re.search('ie*k', 'ieeek')
<_sre.SRE_Match object at 0x01570CD0>
>>>

This pattern symbol is commonly used for handling words that have a common misspelling or variations in language spellings. For example, if you need to write a script that may be used by people speaking either American or British English, you could write this:

>>> re.search('colou*r', 'I bought a new color TV')
<_sre.SRE_Match object at 0x0154FC28>
>>> re.search('colou*r', 'I bought a new colour TV')
<_sre.SRE_Match object at 0x01570C98>
>>>

The u* in the pattern indicates that the letter u may or may not appear in the text to match the pattern.

Another handy feature is combining the dot special character with the asterisk special character. This combination provides a pattern to match any number of any characters. It’s often used between two strings that may or may not appear next to each other in the text:

>>> re.search('regular.*expression', 'This is a regular pattern expression')
<_sre.SRE_Match object at 0x0154FC28>
>>>

By using this pattern, you can easily search for multiple words that may appear anywhere in the text.

Using Advanced Regular Expressions Features

Because Python supports extended regular expressions, you have a few more tools available to you. The following sections show what they are.

The Question Mark

The question mark is similar to the asterisk, but with a slight twist. The question mark indicates that the preceding character can appear zero times or once, but that’s all. It doesn’t match repeating occurrences of the character. In this example, if the e character doesn’t appear in the text, or as long as it appears only once in the text, the pattern matches:

>>> re.search('be?t', 'bt')
<_sre.SRE_Match object at 0x01570CD0>
>>> re.search('be?t', 'bet')
<_sre.SRE_Match object at 0x0154FC28>
>>> re.search('be?t', 'beet')
>>>

The Plus Sign

The plus sign is another pattern symbol that’s similar to the asterisk, but with a different twist than the question mark. The plus sign indicates that the preceding character can appear one or more times, but it must be present at least once. The pattern doesn’t match if the character is not present. In the following example, if the e character is not present, the pattern match fails:

>>> re.search('be+t', 'bt')
>>> re.search('be+t', 'bet')
<_sre.SRE_Match object at 0x01570C98>
>>> re.search('be+t', 'beet')
<_sre.SRE_Match object at 0x0154FC28>
>>> re.search('be+t', 'beeet')
<_sre.SRE_Match object at 0x01570C98>
>>>

Using Braces

By using curly braces in Python regular expressions, you can specify a limit on a repeatable regular expression. This is often referred to as an interval. You can express the interval in two formats:

Image {m}The regular expression appears exactly m times.

Image {m,n}The regular expression appears at least m times but no more than n times.

This feature allows you to fine-tune how many times you allow a character (or character class) to appear in a pattern. In this example, the e character can appear once or twice for the pattern match to pass; otherwise, the pattern match fails:

>>> re.search('be{1,2}t', 'bt')
>>> re.search('be{1,2}t', 'bet')
<_sre.SRE_Match object at 0x0154FC28>
>>> re.search('be{1,2}t', 'beet')
<_sre.SRE_Match object at 0x01570C98>
>>> re.search('be{1,2}t', 'beeet')
>>>

The Pipe Symbol

The pipe symbol allows you to specify two or more patterns that the regular expression engine uses in a logical OR formula when examining the data stream. If any of the patterns match the data stream text, the text passes. If none of the patterns match, the data stream text fails.

This is the syntax for using the pipe symbol:

expr1|expr2|...

Here’s an example of this:

>>> re.search('cat|dog', 'The cat is sleeping')
<_sre.SRE_Match object at 0x0154FC28>
>>> re.search('cat|dog', 'The dog is sleeping')
<_sre.SRE_Match object at 0x01570C98>
>>> re.search('cat|dog', 'The horse is sleeping')
>>>

This example looks for the regular expression cat or dog in the data stream.

You can’t place any spaces within the regular expressions and the pipe symbol, or they’ll be added to the regular expression pattern.

Grouping Expressions

Regular expression patterns can be grouped using parenthesis. When you group a regular expression pattern, the group is treated like a standard character. You can apply a special character to the group just as you would to a regular character. Here’s an example:

>>> re.search('Sat(urday)?', 'Sat')
<_sre.SRE_Match object at 0x00B07960>
>>> re.search('Sat(urday)?', 'Saturday')
<_sre.SRE_Match object at 0x015567E0>
>>>

The grouping of the day ending along with the question mark allows the pattern to match either the full day name or the abbreviated name.

It’s common to use grouping along with the pipe symbol to create groups of possible pattern matches, as shown here:

>>> re.search('(c|b)a(b|t)', 'cab')
<_sre.SRE_Match object at 0x015493C8>
>>> re.search('(c|b)a(b|t)', 'cat')
<_sre.SRE_Match object at 0x0157CCC8>
>>> re.search('(c|b)a(b|t)', 'bat')
<_sre.SRE_Match object at 0x015493C8>
>>> re.search('(c|b)a(b|t)', 'tab')
>>>

The pattern (c|b)a(b|t) matches any combination of the letters in the first group along with any combination of the letters in the second group.

Working with Regular Expressions in Your Python Scripts

It helps to actually see regular expressions in use to get a feel for how to use them in your own Python scripts. Just looking at the quirky formats doesn’t help much; seeing some examples of how regular expressions can match real data can help clear things up!

Summary

If you manipulate data in Python scripts, you need to become familiar with regular expressions. A regular expression defines a pattern template that’s used to filter text in a string value. The pattern consists of a combination of standard text characters and special characters. The regular expression engine uses the special characters to match a series of one or more characters. Python uses the re module to provide a platform for using regular expressions in Python scripts. You can use the match(), search(), findall(), and finditer() functions to filter text from string values in your Python scripts using regular expression patterns.

In the next hour, we’ll take a look at how to use exceptions in your Python code. With exceptions, you can add code to your program to handle if things go wrong while the program is running!

Q&A

Q. Do regular expressions work in all language characters?

A. Yes, because Python uses Unicode strings, you can use characters from any language in your regular expression patterns.

Q. Is there a source for common regular expressions?

A. The www.regular-expressions.info website contains lots of different expressions for matching all sorts of data!

Q. Can I save a regular expression test to use in other programs?

A. Yes, you can create a function (see Hour 12, “Creating Functions”) that checks text using your regular expression. You can then copy the function into a module and use that in any program where you need to validate that type of data!

Workshop

Quiz

1. What regular expression character matches text at the end of a string?

a. the caret (^)

b. the dollar sign ($)

c. the dot (.)

d. the question mark (?)

2. The caret special character performs the same function in a regular expression as the match() Python function. True or false?

3. What regular expression pattern should you use to match both the words Charlie and Charles?

Answers

1. b. The dollar sign ($) anchors the expression at the end of the string.

2. True. You may find it easier to use the match() Python function; however, there are plenty of standard regular expressions that use the caret. You can use either format to accomplish the same thing!

3. 'Charl[ie]+[es]+' This regular expression will match if either the "ie" or "es" characters are at the end of the "Charl" string.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.176.14