Regular common syntax
Single character matching
. matches any character except the newline character.
[... ]For matching a character set, such as [A-Za-z0-9] for matching all letters and numbers. [^... ]Indicates that any character other than the one specified in the character set set is matched. For example, [^ 0-9] means to match all characters except numbers.
\Escape character, used to change the original meaning of a special character (so that it represents itself).
Predefined character set
\d for number
\D is not a number
\s for white space
\S for non white space characters
\w for letters and numbers
\W for non alphanumeric
Character times match
*Match previous character 0 or infinite times
+Match previous character 1 or infinite times
? match previous character 0 or 1 times
Boundary matching
^Match beginning of string
$match string end
Grouping
(... Grouping
(? P) group, and specify the NAME of the group as NAME.
(? P=NAME) refers to the string matched by the group with NAME, in combination with the previous use
Exercises 1
import re
import requests
url = "http://qwd.jd.com/fcgi-bin/qwd_searchitem_ex?skuid=26878432382%7C1658610413%7C26222795271%7C25168000024%7C11731514723%7C26348513019%7C20000220615%7C4813030%7C25965247088%7C5327182%7C19588651151%7C1780924%7C15495544751%7C10114188069%7C27036535156%7C10123099847%7C26016197600%7C10503200866%7C16675691362%7C15904713681"
session = requests.session()
r = session.get(url)
html = r.text
# print(html)
reg = re.compile(r"\"skuid\":\"(\d+)\",\s+\"\S+\s+\"skuurl\"\S+\s+\"skuimgurl\":\"(\S+)\",")
result = reg.findall(html)
print(result)
import codecs
import re
import os
regUpstream = re.compile(r"\s*(upstream\s+(\S+)\s+{[^}]+})")
with codecs.open("ga10.wms5.jd.com.txt") as fu:
textList = regUpstream.findall(fu.read())
if not os.path.exists("upstream"):
os.mkdir("upstream")
os.chdir("upstream")
for item in textList:
with codecs.open(item[1], "w") as fw:
fw.write(item[0])
os.chdir("..")
Exercise two
regLocation = re.compile(r"(location\s+/(\S+)/\s+{\s+proxy_next_upstream.*[^]]*?})")
with codecs.open("ga10.wms5.jd.com.txt") as fl:
textLocation = regLocation.findall(fl.read())
if not os.path.exists("location"):
os.mkdir("location")
os.chdir("location")
for each in textLocation:
file = each[1] + ".locaion.conf"
with codecs.open(file, "w") as flw:
flw.write(each[0])