我真的是个菜鸡,做正则的题目没头绪~
找规律没找到。
ps: 这些习题没有答案,好像是个开放式作业。[Python] 纯文本查看 复制代码 import re
# a) Delete all pair of parentheses, unless they contain a parentheses character.
str1 = 'def factorial()'
str2 = 'a/b(division) + c%d(#modulo) - (e+(j/k-3)*4)'
str3 = 'Hi there(greeting). Nice day(a(b)'
remove_parentheses = re.compile(r'') ##### add your solution here
print(remove_parentheses.sub('', str1))
# 返回 'def factorial'
print(remove_parentheses.sub('', str2))
# 返回 'a/b + c%d - (e+*4)'
print(remove_parentheses.sub('', str3))
# 返回 'Hi there. Nice day(a'
# b) Extract all hex character sequences, with optional prefix.
# Match the characters case insensitively, and the sequences shouldn't be surrounded by other word characters.
hex_seq = re.compile(r'') ##### add your solution here
str1 = '128A foo 0xfe32 34 0xbar'
##### add your solution here
# 返回 ['128A', '0xfe32', '34']
str2 = '0XDEADBEEF place 0x0ff1ce bad'
#### add your solution here
# 返回 ['0XDEADBEEF', '0x0ff1ce', 'bad']
# c) Check if input string contains any number sequence that is greater than 624.
str1 = 'hi0000432abcd'
##### add your solution here
# 返回 False
str2 = '42_624 0512'
##### add your solution here
# 返回 False
str3 = '3.14 96 2 foo1234baz'
##### add your solution here
# 返回 True
# d) Split the given strings based on consecutive sequence of digit or whitespace characters.
str1 = 'lion \t Ink32onion Nice'
str2 = '**1\f2\n3star\t7 77\r**'
expr = re.compile() ##### add your solution here
print(expr.split(str1))
# 返回 ['lion', 'Ink', 'onion', 'Nice']
print(expr.split(str2))
# 返回 ['**', 'star', '**'] |