by BehindJava

A brief overview on Regular Expressions with few examples

Home » python » A brief overview on Regular Expressions with few examples

In this tutorial we are going to learn about Regular Expressions with few examples.

Regular Expressions (sometimes called regex for short) allows a user to search for strings using almost any sort of rule they can come up. For example, finding all capital letters in a string, or finding a phone number in a document.

Regular expressions are notorious for their seemingly strange syntax. This strange syntax is a byproduct of their flexibility. Regular expressions have to be able to filter out any string pattern you can imagine, which is why they have a complex string pattern format.

Let’s begin by explaining how to search for basic patterns in a string!

Searching for Basic Patterns

Let’s imagine that we have the following string:

text = "The person's phone number is 408-555-1234. Call soon!"

We’ll start off by trying to find out if the string “phone” is inside the text string. Now we could quickly do this with:

'phone' in text
True

But let’s show the format for regular expressions, because later on we will be searching for patterns that won’t have such a simple solution.

import re
pattern = 'phone'
re.search(pattern,text)
<_sre.SRE_Match object; span=(13, 18), match='phone'>

pattern = "NOT IN TEXT"
re.search(pattern,text)

Now we’ve seen that re.search() will take the pattern, scan the text, and then returns a Match object. If no pattern is found, a None is returned (in Jupyter Notebook this just means that nothing is output below the cell).

Let’s take a closer look at this Match object.

pattern = 'phone'
match = re.search(pattern,text)
match
<_sre.SRE_Match object; span=(13, 18), match='phone'>

Notice the span, there is also a start and end index information.

match.span()
(13, 18)

match.start()
13

match.end()
18

But what if the pattern occurs more than once?

text = "my phone is a new phone"
match = re.search("phone",text)
match.span()
(3, 8)

Notice it only matches the first instance. If we wanted a list of all matches, we can use .findall() method:

matches = re.findall("phone",text)
matches
['phone', 'phone']

len(matches)
2

To get actual match objects, use the iterator:

for match in re.finditer("phone",text):
    print(match.span())
(3, 8)
(18, 23)

If you wanted the actual text that matched, you can use the .group() method.

match.group()
'phone'

Patterns

So far, we’ve learned how to search for a basic string. What about more complex examples? Such as trying to find a telephone number in a large string of text? Or an email address?

We could just use search method if we know the exact phone or email, but what if we don’t know it? We may know the general format, and we can use that along with regular expressions to search the document for strings that match a particular pattern.

This is where the syntax may appear strange at first, but take your time with this, often its just a matter of looking up the pattern code.

Let’s begin!

Identifiers for Characters in Patterns Characters such as a digit or a single string have different codes that represent them. You can use these to build up a pattern string. Notice how these make heavy use of the backwards slash \ . Because of this when defining a pattern string for regular expression we use the format:

r'mypattern'

placing the r in front of the string allows python to understand that the \ in the pattern string are not meant to be escape slashes.

Below you can find a table of all the possible identifiers:

For example:

text = "My telephone number is 408-555-1234"
phone = re.search(r'\d\d\d-\d\d\d-\d\d\d\d',text)
phone.group()
'408-555-1234'

Notice the repetition of \d. That is a bit of an annoyance, especially if we are looking for very long strings of numbers. Let’s explore the possible quantifiers.

Quantifiers

Now that we know the special character designations, we can use them along with quantifiers to define how many we expect.

Operator Description Example Pattern Code Example Match
\d A digit file_\d\d file_25
\w Alphanumeric \w-\w\w\w A-b_1
\s White space a\sb\sc a b c
\D A non digit \D\D\D ABC
\W Non-alphanumeric \W\W\W\W\W *-+=)
\S Non-whitespace \S\S\S\S Yoyo

Groups

What if we wanted to do two tasks, find phone numbers, but also be able to quickly extract their area code (the first three digits). We can use groups for any general task that involves grouping together regular expressions (so that we can later break them down).

Using the phone number example, we can separate groups of regular expressions using parenthesis:

phone_pattern = re.compile(r'(\d{3})-(\d{3})-(\d{4})')
results = re.search(phone_pattern,text)
# The entire result
results.group()
'408-555-1234'
# Can then also call by group position.
# remember groups were separated by parenthesis ()
# Something to note is that group ordering starts at 1. Passing in 0 returns everything
results.group(1)
'408'
results.group(2)
'555'
results.group(3)
'1234'
# We only had three groups of parenthesis
results.group(4)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-32-866de7a94a57> in <module>()
      1 # We only had three groups of parenthesis
----> 2 results.group(4)

IndexError: no such group

Additional Regex Syntax

Or operator |

Use the pipe operator to have an or statment. For example

re.search(r"man|woman","This man was here.")
<_sre.SRE_Match object; span=(5, 8), match='man'>
re.search(r"man|woman","This woman was here.")
<_sre.SRE_Match object; span=(5, 10), match='woman'>

The Wildcard Character Use a “wildcard” as a placement that will match any character placed there. You can use a simple period . for this. For example:

re.findall(r".at","The cat in the hat sat here.")
['cat', 'hat', 'sat']

re.findall(r".at","The bat went splat")
['bat', 'lat']

Notice how we only matched the first 3 letters, that is because we need a . For each wildcard letter. Or use the quantifiers described above to set its own rules.

re.findall(r"...at","The bat went splat")
['e bat', 'splat']

However, this still leads the problem to grabbing more beforehand. Really, we only want words that end with “at”.

# One or more non-whitespace that ends with 'at'
re.findall(r'\S+at',"The bat went splat")
['bat', 'splat']

Starts with and Ends With We can use the ^ to signal starts with, and the $ to signal ends with:

# Ends with a number
re.findall(r'\d$','This ends with a number 2')
['2']
# Starts with a number
re.findall(r'^\d','1 is the loneliest number.')
['1']

Note that this is for the entire string, not individual words!

Exclusion To exclude characters, we can use the ^ symbol in conjunction with a set of brackets []. Anything inside the brackets is excluded. For example:

phrase = "there are 3 numbers 34 inside 5 this sentence."
re.findall(r'[^\d]',phrase)
['t',
 'h',
 'e',
 'r',
 'e',
 ' ',
 'a',
 'r',
 'e',
 ' ',
 ' ',
 'n',
 'u',
 'm',
 'b',
 'e',
 'r',
 's',
 ' ',
 ' ',
 'i',
 'n',
 's',
 'i',
 'd',
 'e',
 ' ',
 ' ',
 't',
 'h',
 'i',
 's',
 ' ',
 's',
 'e',
 'n',
 't',
 'e',
 'n',
 'c',
 'e',
 '.']

To get the words back together, use a + sign

re.findall(r'[^\d]+',phrase)
['there are ', ' numbers ', ' inside ', ' this sentence.']

We can use this to remove punctuation from a sentence.

test_phrase = 'This is a string! But it has punctuation. How can we remove it?'
re.findall('[^!.? ]+',test_phrase)
['This',
 'is',
 'a',
 'string',
 'But',
 'it',
 'has',
 'punctuation',
 'How',
 'can',
 'we',
 'remove',
 'it']
clean = ' '.join(re.findall('[^!.? ]+',test_phrase))
clean
'This is a string But it has punctuation How can we remove it'

Brackets for Grouping

As we showed above we can use brackets to group together options, for example if we wanted to find hyphenated words:

text = 'Only find the hypen-words in this sentence. But you do not know how long-ish they are'
re.findall(r'[\w]+-[\w]+',text)
['hypen-words', 'long-ish']

Parenthesis for Multiple Options If we have multiple options for matching, we can use parenthesis to list out these options. For Example:

# Find words that start with cat and end with one of these options: 'fish','nap', or 'claw'
text = 'Hello, would you like some catfish?'
texttwo = "Hello, would you like to take a catnap?"
textthree = "Hello, have you seen this caterpillar?"
re.search(r'cat(fish|nap|claw)',text)
<_sre.SRE_Match object; span=(27, 34), match='catfish'>
re.search(r'cat(fish|nap|claw)',texttwo)
<_sre.SRE_Match object; span=(32, 38), match='catnap'>
# None returned
re.search(r'cat(fish|nap|claw)',textthree)

Conclusion

Excellent work! For full information on all possible patterns, check out: https://docs.python.org/3/howto/regex.html

Character Description Example pattern code Example Match
+ Occurs one or more times Version \w-\w+ Version A-b1_1
{3} Occurs exactly 3 times \D{3} abc
{2,4} Occurs 2 to 4 times \d{2,4} 123
{3,} Occurs 3 or more \w{3,} anycharacters
* Occurs zero or more times A*B*C* AAACC
? Once or none plurals? plural