GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing regexone.com — RegexOne - Learn Regular Expressions - Python 2 All Lessons
Lesson 1: An Introduction, and the ABCs
Lesson 1½: The 123s
Lesson 2: The Dot
Lesson 3: Matching specific characters
Lesson 4: Excluding specific characters
Lesson 5: Character ranges
Lesson 6: Catching some zzz's
Lesson 7: Mr. Kleene, Mr. Kleene
Lesson 8: Characters optional
Lesson 9: All this whitespace
Lesson 10: Starting and ending
Lesson 11: Match groups
Lesson 12: Nested groups
Lesson 13: More group work
Lesson 14: It's all conditional
Lesson 15: Other special characters
Lesson X: Infinity and beyond!
Practice Problems
Problem 1: Matching a decimal numbers
Problem 2: Matching phone numbers
Problem 3: Matching emails
Problem 4: Matching HTML
Problem 5: Matching specific filenames
Problem 6: Trimming whitespace from start and end of line
Problem 7: Extracting information from a log file
Problem 8: Parsing and extracting data from a URL
Problem X: Infinity and beyond!
Using Regular Expressions in Python 2
If you need a refresher on how Regular Expressions work, check out our Interactive Tutorial first! Python supports regular expressions through the standard python library Raw Python stringsWhen writing regular expression in Python, it is recommended that you use raw strings
instead of regular Python strings. Raw strings begin with a special prefix ( This means that a pattern like Matching a stringThe Note that this method stops after the first match, so this is best suited for testing a regular expression more than extracting data. Method
matchObject = re.search(pattern, input_str, flags=0)
Example
import re
# Lets use a regular expression to match a date string. Ignore
# the output since we are just testing if the regex matches.
regex = r"([a-zA-Z]+) (\d+)"
if re.search(regex, "June 24"):
# Indeed, the expression "([a-zA-Z]+) (\d+)" matches the date string
# If we want, we can use the MatchObject's start() and end() methods
# to retrieve where the pattern matches in the input string, and the
# group() method to get all the matches and captured groups.
match = re.search(regex, "June 24")
# This will print [0, 7), since it matches at the beginning and end of the
# string
print "Match at index %s, %s" % (match.start(), match.end())
# The groups contain the matched values. In particular:
# match.group(0) always returns the fully matched string
# match.group(1), match.group(2), ... will return the capture
# groups in order from left to right in the input string
# match.group() is equivalent to match.group(0)
# So this will print "June 24"
print "Full match: %s" % (match.group(0))
# So this will print "June"
print "Month: %s" % (match.group(1))
# So this will print "24"
print "Day: %s" % (match.group(2))
else:
# If re.search() does not match, then None is returned
print "The regex pattern does not match. :("
Capturing groupsUnlike the If you need additional context for each match, you can use Methods
matchList = re.findall(pattern, input_str, flags=0)
matchList = re.finditer(pattern, input_str, flags=0)
Example
import re
# Lets use a regular expression to match a few date strings.
regex = r"[a-zA-Z]+ \d+"
matches = re.findall(regex, "June 24, August 9, Dec 12")
for match in matches:
# This will print:
# June 24
# August 9
# Dec 12
print "Full match: %s" % (match)
# To capture the specific months of each date we can use the following pattern
regex = r"([a-zA-Z]+) \d+"
matches = re.findall(regex, "June 24, August 9, Dec 12")
for match in matches:
# This will now print:
# June
# August
# Dec
print "Match month: %s" % (match)
# If we need the exact positions of each match
regex = r"([a-zA-Z]+) \d+"
matches = re.finditer(regex, "June 24, August 9, Dec 12")
for match in matches:
# This will now print:
# 0 7
# 9 17
# 19 25
# which corresponds with the start and end of each match in the input string
print "Match at index: %s, %s" % (match.start(), match.end())
Finding and replacing stringsAnother common task is to find and replace a part of a string using regular expressions, for example,
to replace all instances of an old email domain, or to swap the order of some text. You
can do this in Python with the The optional Method
replacedString = re.sub(pattern, replacement_pattern, input_str, count, flags=0)
Example
import re
# Lets try and reverse the order of the day and month in a date
# string. Notice how the replacement string also contains metacharacters
# (the back references to the captured groups) so we use a raw
# string for that as well.
regex = r"([a-zA-Z]+) (\d+)"
# This will reorder the string and print:
# 24 of June, 9 of August, 12 of Dec
print re.sub(regex, r"\2 of \1", "June 24, August 9, Dec 12")
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |