By altering our regex pattern, we can find dates that use either a forward slash ( \) or a dash ( –) as the separator. We can also use regex to find dates that are formatted in different ways. The characters d and + tell regex we’re looking for multiple digits within the text. The regex pattern here uses special characters to define the strings we want to extract from the text file. # match a regex pattern for formatted dates Example: Finding formatted dates with regex import re For instance, we can craft a pattern that will find all the formatted dates in the following body of text. The beauty of regular expression is that we can use special characters to create powerful search patterns.
With regular expression, or regex for short, we can search a text by matching a string to a predefined pattern.
How dates are formatted is a factor to consider before we go about extracting them from text files.įor instance, if a date follows the month/date/year format, we can find it using a regular expression pattern.