相关疑难解决方法(0)

如何从python中的正则表达式匹配返回一个字符串?

我正在使用python脚本在文本文件中运行行.我想img在文本文档中搜索标记并将标记作为文本返回.

当我运行正则表达式时,re.match(line)它返回一个 _sre.SRE_MATCH对象.如何让它返回一个字符串?

import sys
import string
import re

f = open("sample.txt", 'r' )
l = open('writetest.txt', 'w')

count = 1

for line in f:
    line = line.rstrip()
    imgtag  = re.match(r'<img.*?>',line)
    print("yo it's a {}".format(imgtag))
Run Code Online (Sandbox Code Playgroud)

运行时打印:

yo it's a None
yo it's a None
yo it's a None
yo it's a <_sre.SRE_Match object at 0x7fd4ea90e578>
yo it's a None
yo it's a <_sre.SRE_Match object at 0x7fd4ea90e578>
yo it's a None
yo it's a …
Run Code Online (Sandbox Code Playgroud)

python regex

56
推荐指数
4
解决办法
8万
查看次数

标签 统计

python ×1

regex ×1