Regular Expressions – Anchors & Wildcards

In this lesson on regular expression, we will learn about the importance of anchors and wildcard characters.

Firstly, we start with anchor characters which are used to represent the start and end of the string. There are two anchor characters ‘^’ and ‘$’. The first anchor character i.e., ‘^’ specifies the start of the string. The character after ‘^’ in the pattern must be the first character of the string in order to match the pattern.

Contrary to the above scenario, ‘$’ specifies the end of the string i.e., the character preceding ‘$’ in the pattern must be the last character of the string in order to match the pattern.

In addition, we can specify both the anchors in a single regular expression itself. For example, the regular expression pattern ‘^ab*a$’ will match any string that starts and end with a’s with any number of b’s between them. It will match ‘aba’, ‘abba’, ‘abbbbbbba’, and even ‘aa’ (as ‘*’ matches zero or more b’s). But it will not match the string ‘a’ because there is only one ‘a’ in this string and in the pattern we have specified that there must be two a’s, one at the start and one at the end.

Exercise 1: Anchors

Description

Write a pattern that matches all the dictionary words that start with ‘D’

Positive matches (should match all of these):
Delight
diligence
Danger

Negative match (shouldn’t match any of these):
Bribe
10
Zenith

Regular_Expression_Basics1

As we can observe from the above results that the pattern with the anchor tag matched all the words starting with the letter ‘D’. Further, we have also mentioned the flag parameter as re.I for ignoring the case.

Exercise 2: Anchors

Description

Write a pattern that matches a word that ends with ‘ing’. Words such as ‘dancing’, ‘beating’, ‘raining’, etc. should match while words that don’t have ‘ing’ at the end shouldn’t match.

Regular_Expression_Basics1