码迷,mamicode.com
首页 > 其他好文 > 详细

正则模块,sys模块

时间:2018-06-21 22:25:41      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:规则   imp   out   字母   dfs   expr   非贪婪   arc   模式   

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/ \n"))#匹配任意空格,以及[\t\n\r\f]
# print(re.findall("\S","yf 258 say 136 is boy60 you ni ma king! 68/ \n"))#匹配任意非空格字符
# 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/ \n"))#匹配任意非数字

# []:匹配一个字符,该字符属于中括号内指定的字符
# 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‘,‘a\c a1c aAc aac‘))
# print(re.findall(‘a\\\\c‘,‘a\c 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模块

标签:规则   imp   out   字母   dfs   expr   非贪婪   arc   模式   

原文地址:https://www.cnblogs.com/yf18767106368/p/9210932.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!