python学习-第14课

一、正则表达式练习题

1.将ga10.wms5.jd.com.txt中分别以upstream和location开头的内容筛选出来,并分别生成相应的新文档
ga10.wms5.jd.com.txt链接地址:
https://github.com/ajing2/LivePython1/blob/master/%E7%AC%AC%E5%8D%81%E5%9B%9B%E8%AF%BE/ga10.wms5.jd.com.txt

import codecs, re, os

with codecs.open("ga10.wms5.jd.com.txt", "r") as f1:
    pattern1 = re.compile(r"\s+(upstream\s+(\S+)\s+{[^}]+})")
    ups = pattern1.findall(f1.read())
    if not os.path.exists("upstream"):
        os.mkdir("upstream")
    os.chdir("upstream")
    for lists, title in ups:
        with codecs.open("{0}.upstream.conf".format(title.split(".")[0]), "w") as fu:
            fu.write(lists)
    os.chdir("..")

with codecs.open("ga10.wms5.jd.com.txt", "r") as f2:
    pattern2 = re.compile(r"(location\s+(.*)\s+{\s+proxy[^}]+})")
    locations = pattern2.findall(f2.read())
    if not os.path.exists("location"):
        os.mkdir("location")
    os.chdir("location")
    for contents, title in locations:
        with codecs.open("{0}.location.conf".format(title.split("/")[1]), "w") as fl:
            fl.write(contents)

2.打开url的skuid和skuurl的图片信息
import re
from pip._vendor 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
reg = re.compile(r"\"skuid\":\"(\d+)\",.*?\"skuurl\":\"(\S+)\"")
result = reg.findall(html)
print(result)

3.其他
1)、长度为8-10的用户密码(以字母开头、数字、下划线): ^[a-zA-Z]\w{7,10}$
2)、验证输入只能是汉字 : ^[\u4e00-\u9fa5]{0,}$
3)、电子邮箱验证:^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
4)、URL地址验证:^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$
5)、电话号码的验证:请参考:http://blog.csdn.net/kiritor/article/details/8733469
6)、简单的身份证号验证:\d{15}|\d{18}$


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值