正则表达式的学习过程
#优先使用内置函数
a = 'C|C++|Java|C#|Python|Javascript'
print(a.index('Python')>-1)
print('Python' in a)
#利用内置函数判断字符串'python'是否在a中用正则表达式
import re # 引入re 模块
a = 'C|C++|Java|C#|Python|Javascript'
r = re.findall('Python',a) #findall 方法
print(r)
if len(r) > 0:
print('字符串中包含Python')
else:
print('No')import re
a = 'C0C++7Java12C#9Python67\nJavascript8'
#用r来提取a中的数字
r = re.findall('\d',a) #\d 来表示数字(0-9)
print(r)
#用s来提取a中的非数字
s = re.findall('\D',a) #\D 来表示非数字的字符
print(s)Last updated
Was this helpful?