21
loading...
This website collects cookies to deliver better user experience
This is Day 14 of the #100DaysOfPython challenge
hello-python-regex
directory and install Pillow.# Make the `hello-python-regex` directory
$ mkdir hello-python-regex
$ cd hello-python-regex
# Create a folder to place your icons
$ mkdir icons
# Init the virtual environment
$ pipenv --three
$ pipenv install --dev jupyterlab
# Startup the notebook server
$ pipenv run jupyter-lab
# ... Server is now running on http://localhost:8888/lab
hello-python-regex/docs/regex.ipynb
.import re
m = re.search("Hello, (.+)", "Hello, world!")
m.group(1)
# 'world!'
Scan through string looking for the first location where this regular expression produces a match, and return a corresponding match object. Return None if no position in the string matches the pattern; note that this is different from finding a zero-length match at some point in the string.
import re
pattern = re.compile("ello, (.+)")
m = pattern.search("Hello, world!")
m.group(1)
print(m) # <re.Match object; span=(0, 13), match='Hello, world!'>
print(m.group(1)) # world!
n = pattern.search("Hello, world!", 0)
print(n) # <re.Match object; span=(0, 13), match='Hello, world!'>
print(n.group(1)) # world!
If zero or more characters at the beginning of string match this regular expression, return a corresponding match object. Return None if the string does not match the pattern; note that this is different from a zero-length match.
pattern = re.compile("ello, (.+)")
m = pattern.match("Hello, world!")
# No match as "e" is the 2nd character the "Hello, world!".
print(m) # None
pattern = re.compile("Hello, (.+)")
# Does match
n = pattern.match("Hello, world!")
print(n) # <re.Match object; span=(0, 13), match='Hello, world!'>
re.match
and re.search
as a static method, you can pass the Regex as the first argument:m = re.match("Hello, (.+)", "Hello, world!")
print(m) # <re.Match object; span=(0, 13), match='Hello, world!'>
n = re.match("Hello, (.+)", "Hello, world!")
print(n) # <re.Match object; span=(0, 13), match='Hello, world!'>
m = re.split(",", "Hello, world!")
print(m) # ['Hello', ' world!']
n = re.split("\s", "Hello beautiful world!")
print(n) # ['Hello', 'beautiful', 'world!']
search
and sub
methods to replace matches.# Simple example
target = "Photo credit by [@thomas](https://site.com/@thomas)"
m = re.search(r"Photo credit by \[@(.+)\]\(https://site.com/@(.+)\)", target)
res = re.sub(m.group(1), "dennis", target)
print(res) # Photo credit by [@dennis](https://site.com/@dennis)
# By iterating for multiple matches
target = "Photo credit by [@thomas](https://site.com/@user)"
m = re.search(r"Photo credit by \[@(.+)\]\(https://site.com/@(.+)\)", target)
res = target
for idx, val in enumerate(m.groups()):
res = re.sub(val, "dennis", res)
print(res) # Photo credit by [@dennis](https://site.com/@dennis)
target = """
Other words thomas and user we don't want to replace.
Photo credit by [@thomas](https://site.com/@user)
"""
new_name = "dennis"
pattern = re.compile(r"Photo credit by \[@(.+)\]\(https://site.com/@(.+)\)")
res = pattern.sub(f"Photo credit by [@{new_name}](https://site.com/@{new_name})", target)
# Other words thomas and user we don't want to replace.
# Photo credit by [@dennis](https://site.com/@dennis)
re
module from the standard library to search, match, split and replace text in Python strings.pawel_czerwinski