Regular Expressions Matching and Extracting Data
https://www.youtube.com/embed/LaCZnTbQGkE
>>> import re
>>> x = 'What will search for a "$" in a regular expression?'
>>> y = re.findall('[0-9]+',x)
>>> print(y)
[]
y = re.findall('[0-9]+',x) find every character in x and keep or remember only the digit 0 - 9 might be one digit or more then print out only the digit.
>>> import re
>>> x = 'What will search for a "$" in a regular expression?'
>>> y = re.findall('[0-9]+',x)
>>> print (y)
[]
>>> y = re.findall('[AEIOU]+',x)
>>> print(y)
[]
y = re.findall('[AEIOU]+',x) find AEIOU in words in the x but just keep only upper latter
>>> import re
>>> x = 'Form: Using the : character'
>>> y = re.findall('^F.+:',x)
>>> print(y)
['Form: Using the :']
y = re.findall('^F.+:',x) find the words that start with F then follow by one or more character then end with :
import re
x = 'From: Using the : character'
y = re.findall('^F.+?:',x)
print (y)
Output
['From:']
y = re.findall('^F.+?:',x) find word start with F follow by one or more character but not greedy
Question
-
What will the following program print?:
import re s = 'A message from csev@umich.edu to cwen@iupui.edu about meeting @2PM' lst = re.findall('\\S+@\\S+', s) print(lst) -
Ans
['csev@umich.edu', 'cwen@iupui.edu']