正则模块,sys模块

Posted yf18767106368

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了正则模块,sys模块相关的知识,希望对你有一定的参考价值。

1、什么是正则

    正则就是用一系列具有特殊含义的字符组成一套规则,该规则用来描述具有某一特征的字符串,

    正则就是用来去一个大的字符串中匹配出符合规则的子字符串

 

2、为什么要用正则

    1、用户注册

    2、爬虫程序

 

3、如何用正则

import re
# print(re.findall("w","yf 258 say 136 is boy60 you ni ma king! 68/"))#匹配字母数字及下划线可以对照出到列表
# print(re.findall("W","yf 258 say 136 is boy60 you ni ma king! 68/"))#匹配非字母数字下划线可以显示
# print(re.findall("s","yf 258 say 136 is boy60 you ni ma king! 68/ "))#匹配任意空格,以及[ f]
# print(re.findall("S","yf 258 say 136 is boy60 you ni ma king! 68/ "))#匹配任意非空格字符
# print(re.findall("d","yf 258 say 136 is boy60 you ni ma king! 68/"))#匹配任意数字
# print(re.findall("D","yf 258 say 136 is boy60 you ni ma king! 68/ "))#匹配任意非数字

# []:匹配一个字符,该字符属于中括号内指定的字符
# print(re.findall(‘a..c‘,‘abc a1 c aac asd aaaaac a *c a+c abasd =‘))
# print(re.findall(‘a.c‘,‘abc a1 c aac aAc aBc asd aaaaac a-c a/c a *c a+c abasd = a1c a2c‘))
# print(re.findall(‘a[a-z]c‘,‘abc a1 c aac aAc aBc asd aaaaac a-c a/c a *c a+c abasd = a1c a2c‘))
# print(re.findall(‘a[A-Z]c‘,‘abc a1 c aac aAc aBc asd aaaaac a-c a/c a *c a+c abasd = a1c a2c‘))
# print(re.findall(‘a[-+*/]c‘,‘abc a1 c aac aAc aBc asd aaaaac a-c a/c a *c a+c abasd = a1c a2c‘))
# print(re.findall(‘a[a-z][a-z]c‘,‘abc a1 c aac aAc aBc asd aaaaac a-c a/c a *c a+c abasd = a1c a2c‘))
# print(re.findall(‘a[^a-z]c‘,‘abc a1 c aac aAc aBc asd aaaaac a-c a/c a *c a+c abasd = a1c a2c‘))


# *: 必须与其他字符连用,代表左侧的字符出现0次或者无穷次
# print(re.findall(‘ab*‘,‘a ab abbb abbbb a1bbbb a-123‘))
#                                              ab*
#[‘a‘,‘ab‘,‘abbb‘,‘abbbb‘,‘a‘,‘a‘]
# print(re.findall(‘ab{0,}‘,‘a ab abbb abbbb a1bbbb a-123‘))



# ?: 必须与其他字符连用,代表左侧的字符出现0次或者1次
# print(re.findall(‘ab?‘,‘a ab abbb abbbb a1bbbb a-123‘))
#                                              ab?
#[‘a‘,‘ab‘,‘ab‘,‘ab‘,‘a‘,‘a‘]
# print(re.findall(‘ab{0,1}‘,‘a ab abbb abbbb a1bbbb a-123‘))



# +: 必须与其他字符连用,代表左侧的字符出现1次或者无穷次
# print(re.findall(‘ab+‘,‘a ab abbb abbbb a1bbbb a-123‘))
#                                              ab+
# [‘ab‘,‘abbb‘,‘abbbb‘]
# print(re.findall(‘ab{1,}‘,‘a ab abbb abbbb a1bbbb a-123‘))


# {n,m}: 必须与其他字符连用
# print(re.findall(‘ab{1,3}‘,‘a ab abbb abbbb a1bbbb a-123‘))
#                                                  ab{1,3}
# [‘ab‘,‘abbb‘,‘abbb‘]


# .*:贪婪匹配
# print(re.findall(‘a.*c‘,‘ab123adfc1134124123adasfc123123‘))

# .*?:非贪婪匹配
# print(re.findall(‘a.*?c‘,‘ab123adfc1134124123adasfc123123‘))
#                                            a.*?c


#():分组
# print(re.findall(‘expression="(.*?)"‘,‘expression="1+2+3/4*5" egon="beautiful"‘))
#                                       expression=".*?" 显示整体


# print(re.findall(‘href="(.*?)"‘,‘<p>段落</p><a href="https://www.sb.com">点我啊</a><h1>标题</h1><a href="https://www.sb.com">点我啊</a>‘))



#|:
# print(re.findall(‘a|b‘,‘ab123abasdfaf‘))
#                        a|b

# print(re.findall(‘compan(?:ies|y)‘,‘Too many companies have gone bankrupt, and the next one is my company‘))

#companies   company



# print(re.findall(r‘a\c‘,‘ac a1c aAc aac‘))
# print(re.findall(‘a\\c‘,‘ac a1c aAc aac‘))

# print(re.findall(‘ale(x)‘,‘alex is SB,alex is bigSB‘))
# print(re.search(‘alex‘,‘alex is SB,alex is bigSB‘))
# print(re.search(‘ale(x)‘,‘alex is SB,alex is bigSB‘).group())
# print(re.search(‘abcdefg‘,‘alex is SB,alex is bigSB‘))

# print(re.search(‘^alex‘,‘123alex is SB,alex is bigSB‘))
# print(re.match(‘alex‘,‘123alex is SB,alex is bigSB‘))

# l=‘egon:18:male‘.split(‘:‘)
# print(l)
# l1=re.split(‘[ :/-]‘,‘a-b/c egon:18:male xxx‘)
# print(l1)

# print(re.sub(‘[a-z]+xx‘,‘yxp‘,‘lxx is good,sb is lllxx wxx is good cxx is good‘))
#                                                   [a-z]+xx
#
# pattern=re.compile(‘alex‘)
# print(pattern.findall(‘alex is SB,alex is bigSB‘))
# print(pattern.search(‘alex is SB,alex is bigSB‘))

 

 

# import sys
# sys.path

# sys.argv # 用来接收python解释器执行py文件后跟的参数,空格为分隔符,将原本input形式的交互改为sys.argv索引模式
#例如:python cp.py argv1 argv2 arg3
#sys.argv=[‘cp.py‘,‘argv1‘,‘argv2‘,‘argv3‘]

import sys

# print(sys.argv)
# src_file=input(‘请输入源文件路径:‘)
src_file=sys.argv[1]
# dst_file=input(‘请输入目标文件路径:‘)
dst_file=sys.argv[2]
with open(src_file,‘rb‘) as read_f,
        open(dst_file,‘wb‘) as write_f:
    for line in read_f:
        write_f.write(line)

 

# import os
# os.system(‘tasklist‘)

import subprocess
import time

obj=subprocess.Popen(
    ‘taskliasdfsadfst‘,
    shell=True,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE

)
# print(obj)
# stdout_res=obj.stdout.read()
# print(stdout_res.decode(‘gbk‘))
# print(stdout_res)

stderr_res1=obj.stderr.read()
stderr_res2=obj.stderr.read()
stderr_res3=obj.stderr.read()
# print(stderr_res1.decode(‘gbk‘))
print(stderr_res1)
print(stderr_res2)
print(stderr_res3)

# import time
# time.sleep(50)












































































































































以上是关于正则模块,sys模块的主要内容,如果未能解决你的问题,请参考以下文章

python_way day6 反射,正则 模块

python学习第十九天:re,sys,suprocess模块

python之模块初识-sys模块

part10-1 Python常见模块(sys模块os模块)

python ossys模块时间模块正则表达式

python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess