自定义函数实现range;re模块的部分使用

一:

方法一:

class MyIterator:
    def __init__(self, start=None, stop=None, step=1):
        if stop is None:
            self.stop = start
            self.start = 0
        else:
            self.stop = stop
            self.start = start
        self.step = step
        if self.step > 0:
            self.data = self.stop
        else:
            self.data = self.start
    def __iter__(self):
        return self

    def __next__(self):
        n = True
        if(self.start < self.stop) == n:
            self.start = self.start + self.step
            return self.start - self.step
        else:
            raise StopIteration


for i in MyIterator(10):
    print(i)
print()

for i in MyIterator(0, 10):
    print(i)
print()

for i in MyIterator(0, 10, 2):
    print(i)
print()

方法二: 

def my_iterator(*args):
    start, stop, step = 0, 1, 1
    if not args:
        raise TypeError('TypeError: my_iterator expected at least 1 arguments, got 0')
    elif len(args) > 3:
        raise TypeError("TypeError: my_iterator expected at most 3 arguments, got {}".format(len(args)))
    if len(args) == 1:
        stop = args[0]
    if len(args) == 2:
        start, stop = args
    if len(args) == 3:
        start, stop, step = args

    while (stop - start) * step > 0:
        yield start
        start += step


for i in my_iterator(10, 20):
    print(i)
print()
for i in my_iterator(10):
    print(i)
print()
for i in my_iterator(10, 0, -2):
    print(i)

运行结果:

法一:

D:\Python-interpreter\python.exe D:/Python-code/main.py
0
1
2
3
4
5
6
7
8
9

0
1
2
3
4
5
6
7
8
9

0
2
4
6
8


Process finished with exit code 0

法二: 

D:\Python-interpreter\python.exe D:/Python-code/main.py
10
11
12
13
14
15
16
17
18
19

0
1
2
3
4
5
6
7
8
9

10
8
6
4
2

Process finished with exit code 0

正则表达式:

pattern:正则表达式(匹配的对象)
string:待匹配的字符串
flags:模式(匹配的条件)

match:匹配

import re
str_test = "abcd"
pattern = "abc"
match_obj = re.match(pattern, str_test )
print(match_obj)

结果:

D:\Python-interpreter\python.exe D:/Python-code/main.py
<re.Match object; span=(0, 3), match='abc'>

Process finished with exit code 0

fullmatch:完全匹配,只有当 pattern—匹配的对象和 string—要匹配的字符串完全一样时返回Match object,否则为空

import re
str_test = "abcd123"
pattern = "abc"
test = "abc"
fullmatch_obj1 = re.fullmatch(pattern, str_test)
fullmatch_obj2 = re.fullmatch(pattern, test)
print(fullmatch_obj1)
print(fullmatch_obj2)

结果:

D:\Python-interpreter\python.exe D:/Python-code/main.py
None
<re.Match object; span=(0, 3), match='abc'>

Process finished with exit code 0

search:通过扫描字符串查找一个和正则表达式匹配的内容,返回Match objec,匹配不到为空

import re
str_test = "abcd123"
pattern = "abc"
search_obj = re.search(pattern, str_test)
print(search_obj)
# fullmatch_obj = re

结果:

D:\Python-interpreter\python.exe D:/Python-code/main.py
<re.Match object; span=(0, 3), match='abc'>

Process finished with exit code 0

findall:找到所有,返回一个列表(匹配到的所有结果)

import re
str_test = "abcd123adef"
pattern = "a"
findall_obj = re.findall(pattern, str_test)
print(findall_obj)

结果:

D:\Python-interpreter\python.exe D:/Python-code/main.py
['a', 'a']

Process finished with exit code 0

finditer:返回一个迭代器,且迭代器的每一个元素都是Match object

import re
str_test = "abcd123adef"
pattern = "a"
finditer_obj = re.finditer(pattern, str_test)
print(finditer_obj)
for i in finditer_obj:
    print(i)

结果:

D:\Python-interpreter\python.exe D:/Python-code/main.py
<callable_iterator object at 0x0000027A78CE3F10>
<re.Match object; span=(0, 1), match='a'>
<re.Match object; span=(7, 8), match='a'>

Process finished with exit code 0

split:分割

"""
split(pattern, string, maxsplit=0, flags=0)
pattern:正则表达式,匹配的对象
string:待匹配的字符串
maxsplit:最大分割次数
flags:模式,匹配的条件
"""
import re
str_test = "汽车,火车,飞机,磁悬浮列车,火箭,轮船"
pattern = ","
split_obj = re.split(pattern, str_test, 4)
print(split_obj)

结果:前面已经分割四次,后面【'火箭,轮船'】未被分割

D:\Python-interpreter\python.exe D:/Python-code/main.py
['汽车', '火车', '飞机', '磁悬浮列车', '火箭,轮船']

Process finished with exit code 0

sub:替换

"""
sub(pattern, repl, string, count=0, flags=0)
pattern:匹配的对象
relp:replace:替换,替换的内容
count:替换的最大次数

"""
import re
str_test = "汽车,火车,飞机,磁悬浮列车,火箭,轮船"
pattern = ","
sub_obj = re.sub(pattern, " - ", str_test, 4)
print(sub_obj)

结果:前面已经替换四次,后面【'火箭,轮船'】中的【,】未被替换

D:\Python-interpreter\python.exe D:/Python-code/main.py
汽车 - 火车 - 飞机 - 磁悬浮列车 - 火箭,轮船

Process finished with exit code 0

subn:sub+number,与sub类似

import re
str_test = "汽车,火车,飞机,磁悬浮列车,火箭,轮船"
pattern = ","
subn_obj = re.subn(pattern, " - ", str_test, 4)
print(subn_obj)

结果:返回一个元组,后面是替换次数

D:\Python-interpreter\python.exe D:/Python-code/main.py
('汽车 - 火车 - 飞机 - 磁悬浮列车 - 火箭,轮船', 4)

Process finished with exit code 0

complie:编译

"""
 compile(pattern, flags=0):
    "Compile a regular expression pattern, returning a Pattern object."
    return _compile(pattern, flags)



def findall(pattern, string, flags=0):
    return _compile(pattern, flags).findall(string)

def split(pattern, string, maxsplit=0, flags=0):
    return _compile(pattern, flags).split(string, maxsplit)

def match(pattern, string, flags=0):
    return _compile(pattern, flags).match(string)

def fullmatch(pattern, string, flags=0):
    return _compile(pattern, flags).fullmatch(string)

def search(pattern, string, flags=0):
    return _compile(pattern, flags).search(string)

def sub(pattern, repl, string, count=0, flags=0):
  return _compile(pattern, flags).sub(repl, string, count)

def subn(pattern, repl, string, count=0, flags=0):
    return _compile(pattern, flags).subn(repl, string, count)
在调用这些方法时都调用了_compile(pattern, flags)
可只执行一次_compile()提高效率

举例如下:
"""
import re
str_test = "abcd"
pattern = "abc"

compile_obj = re.compile(pattern)
data = compile_obj.match(str_test)
print(data)

结果:

D:\Python-interpreter\python.exe D:/Python-code/main.py
<re.Match object; span=(0, 3), match='abc'>

Process finished with exit code 0

Guess you like

Origin blog.csdn.net/weixin_59280309/article/details/120815392