在txt文件中,我想通过正则表达式识别一些url,然后将它们发送到新文件中。 但是不幸的是,我发现正则表达式无法识别readlines()函数read的行。
# coding:utf-8
import re
import json
line1 = ''
text = ''
with open(r'all.log') as f:
lines = f.readlines()
for line in lines:
line1 = line
r = re.match('s',line) # pattern I use The most simple word ,just 's'
if r:
print(r.group())
f.close()
break
print(line1)
print(type(line1))
r = re.match('Hello',line1,re.DOTALL)
# but the same re.match method can be used with a 'content' str
content = 'Hello 1234567 is a number. Regex String'
print(re.match('Hello',content).group())
if r:
print(r.group())
else:
print('you get nothing')`
结果: 你好1234567是一个数字。正则表达式字符串
你好 你什么也没得到 [以0.3秒完成]
print(line1)
# line1 = Hello 1234567 is a number. Regex String
content = 'Hello 1234567 is a number. Regex String'
print(type(line1))
print(type(content))
r_line1 = re.match('Hello',line1)
r_content = re.match('Hello',content)
if r_line1:print(r_line1.group())
else:print('r_line1 get nothing')
if r_content:print(r_content.group())
else:print('r_content get nothing')
result:
Hello 1234567 is a number. Regex String
<type 'str'>
<type 'str'>
r_line1 get nothing
Hello
[Finished in 0.3s]