Пример #1
0
def encry_pass_ph(line):
    '''
    一个加密函数
    如果是数字则保持不变
    如果是小写字母,则按照手机展示的九宫格规则转换为数字
    如果是大写字母,则转换为小写字母的下一位 Z则转换为a
    :param line:
    :return:
    '''
    mes = ''
    nums = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    ch = {
        'a': '2',
        'b': '2',
        'c': '2',
        'd': '3',
        'e': '3',
        'f': '3',
        'g': '4',
        'h': '4',
        'i': '4',
        'j': '5',
        'k': '5',
        'l': '5',
        'm': '6',
        'n': '6',
        'o': '6',
        'p': '7',
        'q': '7',
        'r': '7',
        's': '7',
        't': '8',
        'u': '8',
        'v': '8',
        'w': '9',
        'x': '9',
        'y': '9',
        'z': '9'
    }
    print(mes + '--')
    for i in range(len(line)):
        print(line[i])
        print(line[i] in nums)
        print(bool(re.search('[a-z]', line[i])))
        if line[i] in nums:
            mes += line[i]
        elif bool(re.rearch('[a-z]', line[i])):
            mes += ch[line[i]]
        else:
            if line[i].lower() == 'z':
                mes += 'a'
            else:
                mes += chr(ord(line[i].lower()) + 1)
    return mes
Пример #2
0
# ?	匹配前面的子表达式零次或一次。例如,"do(es)?" 可以匹配 "do" 或 "does" 。? 等价于 {0,1}。
# {n}	n 是一个非负整数。匹配确定的 n 次。
# 例如,'o{2}' 不能匹配 "Bob" 中的 'o',但是能匹配 "food" 中的两个 o。
# {n,}	n 是一个非负整数。至少匹配n 次。例如,'o{2,}' 不能匹配 "Bob" 中的 'o',但能匹配 "foooood" 中的所有 o。'o{1,}' 等价于 'o+'。'o{0,}' 则等价于 'o*'。
# {n,m}	m 和 n 均为非负整数,其中n <= m。最少匹配 n 次且最多匹配 m 次。例如,"o{1,3}" 将匹配 "fooooood" 中的前三个 o。'o{0,1}' 等价于 'o?'。请注意在逗号和两个数之间不能有空格。
# \	将下一个字符标记为一个特殊字符、或一个原义字符、或一个向后引用、或一个八进制转义符。例如,'n' 匹配字符 "n"。'\n' 匹配一个换行符。序列 '\' 匹配 "\" 而 "(" 则匹配 "("。
# ^	匹配输入字符串的开始位置。如果设置了 RegExp 对象的 Multiline 属性,^ 也匹配 '\n' 或 '\r' 之后的位置。
# $	匹配输入字符串的结束位置。如果设置了RegExp 对象的 Multiline 属性,$ 也匹配 '\n' 或 '\r' 之前的位置。
# ?	当该字符紧跟在任何一个其他限制符 (*, +, ?, {n}, {n,}, {n,m}) 后面时,匹配模式是非贪婪的。非贪婪模式尽可能少的匹配所搜索的字符串,而默认的贪婪模式则尽可能多的匹配所搜索的字符串。例如,对于字符串 "oooo",'o+?' 将匹配单个 "o",而 'o+' 将匹配所有 'o'。
# (pattern)	匹配 pattern 并获取这一匹配。所获取的匹配可以从产生的 Matches 集合得到,在VBScript 中使用 SubMatches 集合,在JScript 中则使用 $0…$9 属性。要匹配圆括号字符,请使用 '(' 或 ')'。
# (?:pattern)	匹配 pattern 但不获取匹配结果,也就是说这是一个非获取匹配,不进行存储供以后使用。这在使用 "或" 字符 (¦) 来组合一个模式的各个部分是很有用。例如, 'industr(?:y¦ies) 就是一个比'industry¦industries'更简略的表达式。
# x¦y	匹配 x 或 y。例如,'z¦food' 能匹配 "z" 或 "food"。'(z¦f)ood' 则匹配 "zood" 或 "food"。

import re

rst = re.rearch('',str)
# 2.3 模式修正符
# 所谓模式修正符,即可以在不改变正则表达式的情况下,通过模式修正符改变正则表达式的含义,从而实现一些匹配结果的调整等功能。
# 修饰符	描述
# re.I	使匹配对大小写不敏感
# re.L	做本地化识别(locale-aware)匹配
# re.M	多行匹配,影响 ^ 和 $
# re.S	使 . 匹配包括换行在内的所有字符
# re.U	根据Unicode字符集解析字符。这个标志影响 \w, \W, \b, \B.
# re.X	该标志通过给予你更灵活的格式以便你将正则表达式写得更易于理解。
# 实例:

string = "Python"
pat = "pyt"
rst = re.search(pat,string,re.I) # 第三个参数
print(rst)
Пример #3
0
#Week2-RegularExpression.py

import re
###########
re.search()
hand = open('mbox-short.txt')
for line in hand:
	line = line.strip()
	if line.find('From:')>=0:
		print(line)
	####equals to:
	if re.rearch('From:',line):#return True & False
		print(line)

	if line.startswith('From:')
	####equals to:
	if re.research('^From: ', line)

eg: "X-\S+:" start with"X-", without any space, followed by ":"


re.findall()
eg: "[0-9]+" one or more digit
x = 'My 2 favourite number s are 19 and 42'
y = re.findall('[0-9]+',x)
print(y)
>>>y = ['2','19','42']
y = re.findall('[AEIOU]+',x)
print(y)
>>>y = []
Пример #4
0
#!/usr/bin/env python
# coding=utf-8
import urllib
import urllib2
import re
page = 4
url = 'http://www.qiushibaike.com/hot/page/' + str(page)
user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64)'
headers = {'User-Agent': user_agent}
try:
    request = urllib2.Request(url, headers=headers)
    response = urllib2.urlopen(request)
    content = response.read().decode('utf-8')
    pattern = re.compile(
        '<div.*?author">.*?<a.*?<img.*?>(.*?)</a>.*?<div.*?' +
        'content">(.*?)<!--(.*?)-->.*?</div>(.*?)<div class="stats.*?class="number">(.*?)</i>',
        re.S)
    items = re.findall(pattern, content)
    for item in items:
        haveImg = re.rearch("img", item[3])
        if not haveImg:
            print item[0], item[1], item[2], item[4]
#    print response.read()
except urllib2.URLError, e:
    if hasattr(e, "code"):
        print e.code
    if hasattr(e, "reason"):
        print e.reason
Пример #5
0
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> line = "Cats are smarter than dogs"
>>> line
'Cats are smarter than dogs'
>>> import re
>>> re.match(r'dogs', line, re.M|re.I)
>>> re.match(r'Cats', line, re.M|re.I)
<_sre.SRE_Match object at 0x016AA988>
>>> matchObje = re.match(r'Cats', line, re.M|re.I)
>>> matchObje.group()
'Cats'
>>> matchObj = re.rearch(r'dogs', line, re.M|re.I)

Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    matchObj = re.rearch(r'dogs', line, re.M|re.I)
AttributeError: 'module' object has no attribute 'rearch'
>>> matchObje = re.rearch(r'dogs', line, re.M|re.I)

Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    matchObje = re.rearch(r'dogs', line, re.M|re.I)
AttributeError: 'module' object has no attribute 'rearch'
>>> matchObj = re.search(r'dogs', line, re.M|re.I)
>>> print matchObj.group()
dogs
>>> matchObje
<_sre.SRE_Match object at 0x01FEB4F0>
>>> matchObje = re.search(r'dogs',line)
>>> matchObje