要编写一个正则表达式来匹配以1结尾的行,可以使用以下正则表达式:
^.*1$
这个正则表达式的含义是:以任意字符开头(^.*
),并以1结尾(1$
)。
下面是一个使用Python的re模块来匹配以1结尾的行的示例代码:
import re
lines = [
"This line ends with 1",
"This line ends with 2",
"This line ends with 3",
"This line ends with 1",
"This line ends with 4"
]
pattern = r'^.*1$'
for line in lines:
if re.match(pattern, line):
print(line)
运行以上代码会输出以下结果:
This line ends with 1
This line ends with 1
这说明只有以1结尾的行会被匹配到。
上一篇:保留页面之间的表单数值
下一篇:保留已读副本并删除重复项