%22%3E%3Cpath%20d%3D%22m-369%200h408v79h-408z%22%20fill%3D%22%237933ff%22%2F%3E%3Cpath%20d%3D%22m-148%200h306v45h-306z%22%20fill%3D%22%23974dff%22%2F%3E%3C%2Fg%3E%3C%2Fsvg%3E)
%22%2F%3E%3C%2FclipPath%3E%3Cg%20clip-path%3D%22url(%23a)%22%3E%3Cpath%20d%3D%22m76%200h484v36h-484z%22%20fill%3D%22%23974dff%22%2F%3E%3Cpath%20d%3D%22m0%200h384v24h-384z%22%20fill%3D%22%2306bdfc%22%20transform%3D%22matrix(1%200%200%20-1%200%2060)%22%2F%3E%3C%2Fg%3E%3C%2Fsvg%3E)
If you want to take our free Intro to R course, here is the link.
Extracting numbers from strings is a common task, particularly when working with unstructured data or log files.
Say you have the following string: 'the recipe calls for 6 strawberries and 2 bananas'.
It would be useful to extract the 6 and the 2 from this string to be saved for later use when comparing strawberry to banana ratios.
When using a regular expression to extract multiple numbers (or multiple pattern matches, to be exact), you can use the re.findall() function. Dan did not discuss this in the video, but it is straightforward to use: You pass in a pattern and a string to re.findall(), and it will return a list of the matches.
Instructions
- Import
re.
- Write a pattern that will find all the numbers in the following string:
'the recipe calls for 10 strawberries and 1 banana'. To do this:
- Use the
re.findall() function and pass it two arguments: the pattern, followed by the string.
\d is the pattern required to find digits. This should be followed with a + so that the previous element is matched one or more times. This ensures that 10 is viewed as one number and not as 1 and 0.
- Print the matches to confirm that your regular expression found the values
10 and 1.
# Import the regular expression module
____
# Find the numeric values: matches
matches = re.findall('____', '____')
# Print the matches
print(____)
If that makes sense keep going to the next exercise! If not, here is an overview video.
Overview Video on Using Regular Expressions to Clean Strings in Python
Pattern Matching
In this exercise, you'll continue practicing your regular expression skills. For each provided string, your job is to write the appropriate pattern to match it.
Instructions
- Write patterns to match:
- A telephone number of the format
xxx-xxx-xxxx. You already did this in a previous exercise.
- A string of the format: A dollar sign, an arbitrary number of digits, a decimal point, 2 digits.
- Use
\$ to match the dollar sign, \d* to match an arbitrary number of digits, \. to match the decimal point, and \d{x} to match x number of digits.
- A capital letter, followed by an arbitrary number of alphanumeric characters.
- Use
[A-Z] to match any capital letter followed by \w* to match an arbitrary number of alphanumeric characters.
# Write the first pattern
pattern1 = bool(re.match(pattern='____', string='123-456-7890'))
print(pattern1)
# Write the second pattern
pattern2 = bool(re.match(pattern='____', string='$123.45'))
print(pattern2)
# Write the third pattern
pattern3 = bool(re.match(pattern='____', string='Australia'))
print(pattern3)
If you want to learn more from this course, here is the link.
Check out DataCamp's Python String Tutorial.
関連

チートシート
Regular expressions (regex or regexp) are a pattern of characters that describe an amount of text. Regular expressions are one of the most widely used tools in natural language processing and allow you to supercharge common text data manipulation tasks.
%22%3E%0A%20%20%20%20%20%20%20%20%3Crect%20fill%3D%22%23EFEBE4%22%20height%3D%22180%22%20width%3D%22336%22%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Crect%0A%20%20%20%20%20%20%20%20%20%20fill%3D%22%23FF931E%22%0A%20%20%20%20%20%20%20%20%20%20height%3D%22106.6%22%0A%20%20%20%20%20%20%20%20%20%20transform%3D%22rotate(-30%20-208.588%20220.18)%22%0A%20%20%20%20%20%20%20%20%20%20width%3D%22456.125%22%0A%20%20%20%20%20%20%20%20%20%20x%3D%22-208.588%22%0A%20%20%20%20%20%20%20%20%20%20y%3D%22220.18%22%0A%20%20%20%20%20%20%20%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Crect%0A%20%20%20%20%20%20%20%20%20%20fill%3D%22%23F7F3EB%22%0A%20%20%20%20%20%20%20%20%20%20height%3D%2235.875%22%0A%20%20%20%20%20%20%20%20%20%20transform%3D%22rotate(-30%20-208.588%20220.18)%22%0A%20%20%20%20%20%20%20%20%20%20width%3D%22347.576%22%0A%20%20%20%20%20%20%20%20%20%20x%3D%22-208.588%22%0A%20%20%20%20%20%20%20%20%20%20y%3D%22220.18%22%0A%20%20%20%20%20%20%20%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Crect%0A%20%20%20%20%20%20%20%20%20%20fill%3D%22%23000820%22%0A%20%20%20%20%20%20%20%20%20%20height%3D%2235.875%22%0A%20%20%20%20%20%20%20%20%20%20transform%3D%22rotate(-30%20-226.525%20189.111)%22%0A%20%20%20%20%20%20%20%20%20%20width%3D%22347.576%22%0A%20%20%20%20%20%20%20%20%20%20x%3D%22-226.525%22%0A%20%20%20%20%20%20%20%20%20%20y%3D%22189.111%22%0A%20%20%20%20%20%20%20%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Crect%0A%20%20%20%20%20%20%20%20%20%20fill%3D%22%23000820%22%0A%20%20%20%20%20%20%20%20%20%20height%3D%22106.6%22%0A%20%20%20%20%20%20%20%20%20%20transform%3D%22rotate(-30%2085.5879%20256.055)%22%0A%20%20%20%20%20%20%20%20%20%20width%3D%22456.125%22%0A%20%20%20%20%20%20%20%20%20%20x%3D%2285.5879%22%0A%20%20%20%20%20%20%20%20%20%20y%3D%22256.055%22%0A%20%20%20%20%20%20%20%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Crect%0A%20%20%20%20%20%20%20%20%20%20fill%3D%22%23D87300%22%0A%20%20%20%20%20%20%20%20%20%20height%3D%2235.875%22%0A%20%20%20%20%20%20%20%20%20%20transform%3D%22rotate(-30%2085.5879%20256.055)%22%0A%20%20%20%20%20%20%20%20%20%20width%3D%22184.5%22%0A%20%20%20%20%20%20%20%20%20%20x%3D%2285.5879%22%0A%20%20%20%20%20%20%20%20%20%20y%3D%22256.055%22%0A%20%20%20%20%20%20%20%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Crect%0A%20%20%20%20%20%20%20%20%20%20fill%3D%22%23FF931E%22%0A%20%20%20%20%20%20%20%20%20%20height%3D%2235.875%22%0A%20%20%20%20%20%20%20%20%20%20transform%3D%22rotate(-30%20227.432%20132.736)%22%0A%20%20%20%20%20%20%20%20%20%20width%3D%22271.625%22%0A%20%20%20%20%20%20%20%20%20%20x%3D%22227.432%22%0A%20%20%20%20%20%20%20%20%20%20y%3D%22132.736%22%0A%20%20%20%20%20%20%20%20%2F%3E%0A%20%20%20%20%20%20%3C%2Fg%3E%0A%20%20%20%20%20%20%3Cdefs%3E%0A%20%20%20%20%20%20%20%20%3CclipPath%20id%3D%22clip%22%3E%0A%20%20%20%20%20%20%20%20%20%20%3Crect%20fill%3D%22white%22%20height%3D%22180%22%20width%3D%22336%22%20%2F%3E%0A%20%20%20%20%20%20%20%20%3C%2FclipPath%3E%0A%20%20%20%20%20%20%3C%2Fdefs%3E%0A%20%20%20%20%3C%2Fsvg%3E)
チュートリアル
Learn to find and replace strings using regular expressions in Python.
· 2 min%22%3E%0A%20%20%20%20%20%20%20%20%3Crect%20fill%3D%22%2305192D%22%20height%3D%22180%22%20width%3D%22336%22%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Crect%0A%20%20%20%20%20%20%20%20%20%20fill%3D%22%23B08EFF%22%0A%20%20%20%20%20%20%20%20%20%20height%3D%22106.6%22%0A%20%20%20%20%20%20%20%20%20%20transform%3D%22rotate(-30%20-208.588%20220.18)%22%0A%20%20%20%20%20%20%20%20%20%20width%3D%22456.125%22%0A%20%20%20%20%20%20%20%20%20%20x%3D%22-208.588%22%0A%20%20%20%20%20%20%20%20%20%20y%3D%22220.18%22%0A%20%20%20%20%20%20%20%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Crect%0A%20%20%20%20%20%20%20%20%20%20fill%3D%22%237933FF%22%0A%20%20%20%20%20%20%20%20%20%20height%3D%2235.875%22%0A%20%20%20%20%20%20%20%20%20%20transform%3D%22rotate(-30%20-208.588%20220.18)%22%0A%20%20%20%20%20%20%20%20%20%20width%3D%22347.576%22%0A%20%20%20%20%20%20%20%20%20%20x%3D%22-208.588%22%0A%20%20%20%20%20%20%20%20%20%20y%3D%22220.18%22%0A%20%20%20%20%20%20%20%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Crect%0A%20%20%20%20%20%20%20%20%20%20fill%3D%22%2372E5FE%22%0A%20%20%20%20%20%20%20%20%20%20height%3D%2235.875%22%0A%20%20%20%20%20%20%20%20%20%20transform%3D%22rotate(-30%20-226.525%20189.111)%22%0A%20%20%20%20%20%20%20%20%20%20width%3D%22347.576%22%0A%20%20%20%20%20%20%20%20%20%20x%3D%22-226.525%22%0A%20%20%20%20%20%20%20%20%20%20y%3D%22189.111%22%0A%20%20%20%20%20%20%20%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Crect%0A%20%20%20%20%20%20%20%20%20%20fill%3D%22%235EB1FF%22%0A%20%20%20%20%20%20%20%20%20%20height%3D%22106.6%22%0A%20%20%20%20%20%20%20%20%20%20transform%3D%22rotate(-30%2085.5879%20256.055)%22%0A%20%20%20%20%20%20%20%20%20%20width%3D%22456.125%22%0A%20%20%20%20%20%20%20%20%20%20x%3D%2285.5879%22%0A%20%20%20%20%20%20%20%20%20%20y%3D%22256.055%22%0A%20%20%20%20%20%20%20%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Crect%0A%20%20%20%20%20%20%20%20%20%20fill%3D%22%23B08EFF%22%0A%20%20%20%20%20%20%20%20%20%20height%3D%2235.875%22%0A%20%20%20%20%20%20%20%20%20%20transform%3D%22rotate(-30%2085.5879%20256.055)%22%0A%20%20%20%20%20%20%20%20%20%20width%3D%22184.5%22%0A%20%20%20%20%20%20%20%20%20%20x%3D%2285.5879%22%0A%20%20%20%20%20%20%20%20%20%20y%3D%22256.055%22%0A%20%20%20%20%20%20%20%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Crect%0A%20%20%20%20%20%20%20%20%20%20fill%3D%22%2372E5FE%22%0A%20%20%20%20%20%20%20%20%20%20height%3D%2235.875%22%0A%20%20%20%20%20%20%20%20%20%20transform%3D%22rotate(-30%20227.432%20132.736)%22%0A%20%20%20%20%20%20%20%20%20%20width%3D%22271.625%22%0A%20%20%20%20%20%20%20%20%20%20x%3D%22227.432%22%0A%20%20%20%20%20%20%20%20%20%20y%3D%22132.736%22%0A%20%20%20%20%20%20%20%20%2F%3E%0A%20%20%20%20%20%20%3C%2Fg%3E%0A%20%20%20%20%20%20%3Cdefs%3E%0A%20%20%20%20%20%20%20%20%3CclipPath%20id%3D%22clip%22%3E%0A%20%20%20%20%20%20%20%20%20%20%3Crect%20fill%3D%22white%22%20height%3D%22180%22%20width%3D%22336%22%20%2F%3E%0A%20%20%20%20%20%20%20%20%3C%2FclipPath%3E%0A%20%20%20%20%20%20%3C%2Fdefs%3E%0A%20%20%20%20%3C%2Fsvg%3E)
チュートリアル
Discover the power of regular expressions with this tutorial. You will work with the re library, deal with pattern matching, learn about greedy and non-greedy matching, and much more!
· 14 min
チュートリアル
Discover the power of Regular Expressions (RegEx) for pattern matching in Excel. Our comprehensive guide unveils how to standardize data, extract keywords, and perform advanced text manipulations.
· 12 min%22%3E%0A%20%20%20%20%20%20%20%20%3Crect%20fill%3D%22%2372E5FE%22%20height%3D%22180%22%20width%3D%22336%22%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Crect%0A%20%20%20%20%20%20%20%20%20%20fill%3D%22%237933FF%22%0A%20%20%20%20%20%20%20%20%20%20height%3D%22106.6%22%0A%20%20%20%20%20%20%20%20%20%20transform%3D%22rotate(-30%20-208.588%20220.18)%22%0A%20%20%20%20%20%20%20%20%20%20width%3D%22456.125%22%0A%20%20%20%20%20%20%20%20%20%20x%3D%22-208.588%22%0A%20%20%20%20%20%20%20%20%20%20y%3D%22220.18%22%0A%20%20%20%20%20%20%20%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Crect%0A%20%20%20%20%20%20%20%20%20%20fill%3D%22%235EB1FF%22%0A%20%20%20%20%20%20%20%20%20%20height%3D%2235.875%22%0A%20%20%20%20%20%20%20%20%20%20transform%3D%22rotate(-30%20-208.588%20220.18)%22%0A%20%20%20%20%20%20%20%20%20%20width%3D%22347.576%22%0A%20%20%20%20%20%20%20%20%20%20x%3D%22-208.588%22%0A%20%20%20%20%20%20%20%20%20%20y%3D%22220.18%22%0A%20%20%20%20%20%20%20%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Crect%0A%20%20%20%20%20%20%20%20%20%20fill%3D%22%23000820%22%0A%20%20%20%20%20%20%20%20%20%20height%3D%2235.875%22%0A%20%20%20%20%20%20%20%20%20%20transform%3D%22rotate(-30%20-226.525%20189.111)%22%0A%20%20%20%20%20%20%20%20%20%20width%3D%22347.576%22%0A%20%20%20%20%20%20%20%20%20%20x%3D%22-226.525%22%0A%20%20%20%20%20%20%20%20%20%20y%3D%22189.111%22%0A%20%20%20%20%20%20%20%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Crect%0A%20%20%20%20%20%20%20%20%20%20fill%3D%22%237933FF%22%0A%20%20%20%20%20%20%20%20%20%20height%3D%22106.6%22%0A%20%20%20%20%20%20%20%20%20%20transform%3D%22rotate(-30%2085.5879%20256.055)%22%0A%20%20%20%20%20%20%20%20%20%20width%3D%22456.125%22%0A%20%20%20%20%20%20%20%20%20%20x%3D%2285.5879%22%0A%20%20%20%20%20%20%20%20%20%20y%3D%22256.055%22%0A%20%20%20%20%20%20%20%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Crect%0A%20%20%20%20%20%20%20%20%20%20fill%3D%22%235EB1FF%22%0A%20%20%20%20%20%20%20%20%20%20height%3D%2235.875%22%0A%20%20%20%20%20%20%20%20%20%20transform%3D%22rotate(-30%2085.5879%20256.055)%22%0A%20%20%20%20%20%20%20%20%20%20width%3D%22184.5%22%0A%20%20%20%20%20%20%20%20%20%20x%3D%2285.5879%22%0A%20%20%20%20%20%20%20%20%20%20y%3D%22256.055%22%0A%20%20%20%20%20%20%20%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Crect%0A%20%20%20%20%20%20%20%20%20%20fill%3D%22%23000820%22%0A%20%20%20%20%20%20%20%20%20%20height%3D%2235.875%22%0A%20%20%20%20%20%20%20%20%20%20transform%3D%22rotate(-30%20227.432%20132.736)%22%0A%20%20%20%20%20%20%20%20%20%20width%3D%22271.625%22%0A%20%20%20%20%20%20%20%20%20%20x%3D%22227.432%22%0A%20%20%20%20%20%20%20%20%20%20y%3D%22132.736%22%0A%20%20%20%20%20%20%20%20%2F%3E%0A%20%20%20%20%20%20%3C%2Fg%3E%0A%20%20%20%20%20%20%3Cdefs%3E%0A%20%20%20%20%20%20%20%20%3CclipPath%20id%3D%22clip%22%3E%0A%20%20%20%20%20%20%20%20%20%20%3Crect%20fill%3D%22white%22%20height%3D%22180%22%20width%3D%22336%22%20%2F%3E%0A%20%20%20%20%20%20%20%20%3C%2FclipPath%3E%0A%20%20%20%20%20%20%3C%2Fdefs%3E%0A%20%20%20%20%3C%2Fsvg%3E)
チュートリアル
Learn how you can perform various operations on string using built-in Python functions like split, join and regular expressions.
· 2 min%22%3E%0A%20%20%20%20%20%20%20%20%3Crect%20fill%3D%22%23EFEBE4%22%20height%3D%22180%22%20width%3D%22336%22%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Crect%0A%20%20%20%20%20%20%20%20%20%20fill%3D%22%2372E5FE%22%0A%20%20%20%20%20%20%20%20%20%20height%3D%22106.6%22%0A%20%20%20%20%20%20%20%20%20%20transform%3D%22rotate(-30%20-208.588%20220.18)%22%0A%20%20%20%20%20%20%20%20%20%20width%3D%22456.125%22%0A%20%20%20%20%20%20%20%20%20%20x%3D%22-208.588%22%0A%20%20%20%20%20%20%20%20%20%20y%3D%22220.18%22%0A%20%20%20%20%20%20%20%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Crect%0A%20%20%20%20%20%20%20%20%20%20fill%3D%22%23000820%22%0A%20%20%20%20%20%20%20%20%20%20height%3D%2235.875%22%0A%20%20%20%20%20%20%20%20%20%20transform%3D%22rotate(-30%20-208.588%20220.18)%22%0A%20%20%20%20%20%20%20%20%20%20width%3D%22347.576%22%0A%20%20%20%20%20%20%20%20%20%20x%3D%22-208.588%22%0A%20%20%20%20%20%20%20%20%20%20y%3D%22220.18%22%0A%20%20%20%20%20%20%20%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Crect%0A%20%20%20%20%20%20%20%20%20%20fill%3D%22%235EB1FF%22%0A%20%20%20%20%20%20%20%20%20%20height%3D%2235.875%22%0A%20%20%20%20%20%20%20%20%20%20transform%3D%22rotate(-30%20-226.525%20189.111)%22%0A%20%20%20%20%20%20%20%20%20%20width%3D%22347.576%22%0A%20%20%20%20%20%20%20%20%20%20x%3D%22-226.525%22%0A%20%20%20%20%20%20%20%20%20%20y%3D%22189.111%22%0A%20%20%20%20%20%20%20%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Crect%0A%20%20%20%20%20%20%20%20%20%20fill%3D%22%23000820%22%0A%20%20%20%20%20%20%20%20%20%20height%3D%22106.6%22%0A%20%20%20%20%20%20%20%20%20%20transform%3D%22rotate(-30%2085.5879%20256.055)%22%0A%20%20%20%20%20%20%20%20%20%20width%3D%22456.125%22%0A%20%20%20%20%20%20%20%20%20%20x%3D%2285.5879%22%0A%20%20%20%20%20%20%20%20%20%20y%3D%22256.055%22%0A%20%20%20%20%20%20%20%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Crect%0A%20%20%20%20%20%20%20%20%20%20fill%3D%22%23D87300%22%0A%20%20%20%20%20%20%20%20%20%20height%3D%2235.875%22%0A%20%20%20%20%20%20%20%20%20%20transform%3D%22rotate(-30%2085.5879%20256.055)%22%0A%20%20%20%20%20%20%20%20%20%20width%3D%22184.5%22%0A%20%20%20%20%20%20%20%20%20%20x%3D%2285.5879%22%0A%20%20%20%20%20%20%20%20%20%20y%3D%22256.055%22%0A%20%20%20%20%20%20%20%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Crect%0A%20%20%20%20%20%20%20%20%20%20fill%3D%22%235646A5%22%0A%20%20%20%20%20%20%20%20%20%20height%3D%2235.875%22%0A%20%20%20%20%20%20%20%20%20%20transform%3D%22rotate(-30%20227.432%20132.736)%22%0A%20%20%20%20%20%20%20%20%20%20width%3D%22271.625%22%0A%20%20%20%20%20%20%20%20%20%20x%3D%22227.432%22%0A%20%20%20%20%20%20%20%20%20%20y%3D%22132.736%22%0A%20%20%20%20%20%20%20%20%2F%3E%0A%20%20%20%20%20%20%3C%2Fg%3E%0A%20%20%20%20%20%20%3Cdefs%3E%0A%20%20%20%20%20%20%20%20%3CclipPath%20id%3D%22clip%22%3E%0A%20%20%20%20%20%20%20%20%20%20%3Crect%20fill%3D%22white%22%20height%3D%22180%22%20width%3D%22336%22%20%2F%3E%0A%20%20%20%20%20%20%20%20%3C%2FclipPath%3E%0A%20%20%20%20%20%20%3C%2Fdefs%3E%0A%20%20%20%20%3C%2Fsvg%3E)
チュートリアル
Explore regular expressions in R, why they're important, the tools and functions to work with them, common regex patterns, and how to use them.
· 12 min
If you want to take our free Intro to R course, here is the link.
Extracting Numerical Values from Strings
Extracting numbers from strings is a common task, particularly when working with unstructured data or log files.
Say you have the following string:
'the recipe calls for 6 strawberries and 2 bananas'.It would be useful to extract the
6and the2from this string to be saved for later use when comparing strawberry to banana ratios.When using a regular expression to extract multiple numbers (or multiple pattern matches, to be exact), you can use the
re.findall()function. Dan did not discuss this in the video, but it is straightforward to use: You pass in a pattern and a string tore.findall(), and it will return a list of the matches.Instructions
re.'the recipe calls for 10 strawberries and 1 banana'. To do this:re.findall()function and pass it two arguments: the pattern, followed by the string.\dis the pattern required to find digits. This should be followed with a+so that the previous element is matched one or more times. This ensures that10is viewed as one number and not as1and0.10and1.If that makes sense keep going to the next exercise! If not, here is an overview video.
Overview Video on Using Regular Expressions to Clean Strings in Python
Pattern Matching
In this exercise, you'll continue practicing your regular expression skills. For each provided string, your job is to write the appropriate pattern to match it.
Instructions
xxx-xxx-xxxx. You already did this in a previous exercise.\$to match the dollar sign,\d*to match an arbitrary number of digits,\.to match the decimal point, and\d{x}to matchxnumber of digits.[A-Z]to match any capital letter followed by\w*to match an arbitrary number of alphanumeric characters.If you want to learn more from this course, here is the link.
Check out DataCamp's Python String Tutorial.