如何在 string.replace 中输入正则表达式?
- 2024-12-12 08:41:00
- admin 原创
- 130
问题描述:
我需要一些关于声明正则表达式的帮助。我的输入如下:
this is a paragraph with<[1> in between</[1> and then there are cases ... where the<[99> number ranges from 1-100</[99>.
and there are many other lines in the txt files
with<[3> such tags </[3>
所需的输出是:
this is a paragraph with in between and then there are cases ... where the number ranges from 1-100.
and there are many other lines in the txt files
with such tags
我尝试过这个:
#!/usr/bin/python
import os, sys, re, glob
for infile in glob.glob(os.path.join(os.getcwd(), '*.txt')):
for line in reader:
line2 = line.replace('<[1> ', '')
line = line2.replace('</[1> ', '')
line2 = line.replace('<[1>', '')
line = line2.replace('</[1>', '')
print line
我也尝试过这个(但似乎我使用了错误的正则表达式语法):
line2 = line.replace('<[*> ', '')
line = line2.replace('</[*> ', '')
line2 = line.replace('<[*>', '')
line = line2.replace('</[*>', '')
replace
我不想对1 到 99进行硬编码。
解决方案 1:
这个经过测试的代码片段应该可以做到这一点:
import re
line = re.sub(r"</?[d+>", "", line)
编辑:这里有一个注释版本,解释了它是如何工作的:
line = re.sub(r"""
(?x) # Use free-spacing mode.
< # Match a literal '<'
/? # Optionally match a '/'
[ # Match a literal '['
d+ # Match one or more digits
> # Match a literal '>'
""", "", line)
正则表达式很有趣!但我强烈建议您花一两个小时学习基础知识。首先,您需要了解哪些字符是特殊的:“元字符”需要转义(即在前面放置反斜杠 - 并且字符类内部和外部的规则不同。)有一个很棒的在线教程:www.regular-expressions.info。您在那里花费的时间将获得丰厚的回报。祝您使用正则表达式愉快!
解决方案 2:
str.replace()
进行固定替换。改用re.sub()
。
解决方案 3:
我会这样做(正则表达式在评论中解释):
import re
# If you need to use the regex more than once it is suggested to compile it.
pattern = re.compile(r"</{0,}[d+>")
# </{0,}[d+>
#
# Match the character “<” literally «<»
# Match the character “/” literally «/{0,}»
# Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «{0,}»
# Match the character “[” literally «[»
# Match a single digit 0..9 «d+»
# Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
# Match the character “>” literally «>»
subject = """this is a paragraph with<[1> in between</[1> and then there are cases ... where the<[99> number ranges from 1-100</[99>.
and there are many other lines in the txt files
with<[3> such tags </[3>"""
result = pattern.sub("", subject)
print(result)
如果您想了解有关正则表达式的更多信息,我建议您阅读Jan Goyvaerts 和 Steven Levithan 撰写的《正则表达式手册》 。
解决方案 4:
最简单的方法
import re
txt='this is a paragraph with<[1> in between</[1> and then there are cases ... where the<[99> number ranges from 1-100</[99>. and there are many other lines in the txt files with<[3> such tags </[3>'
out = re.sub("(<[^>]+>)", '', txt)
print out
解决方案 5:
字符串对象的 replace 方法不接受正则表达式,只接受固定字符串(参见文档:http ://docs.python.org/2/library/stdtypes.html#str.replace )。
您必须使用re
模块:
import re
newline= re.sub("</?[[0-9]+>", "", line)
解决方案 6:
不必使用正则表达式(对于示例字符串)
>>> s
'this is a paragraph with<[1> in between</[1> and then there are cases ... where the<[99> number ranges from 1-100</[99>.
and there are many other lines in the txt files
with<[3> such tags </[3>
'
>>> for w in s.split(">"):
... if "<" in w:
... print w.split("<")[0]
...
this is a paragraph with
in between
and then there are cases ... where the
number ranges from 1-100
.
and there are many other lines in the txt files
with
such tags
解决方案 7:
import os, sys, re, glob
pattern = re.compile(r"<[d>")
replacementStringMatchesPattern = "<[1>"
for infile in glob.glob(os.path.join(os.getcwd(), '*.txt')):
for line in reader:
retline = pattern.sub(replacementStringMatchesPattern, "", line)
sys.stdout.write(retline)
print (retline)
相关推荐
热门文章
项目管理软件有哪些?
热门标签
云禅道AD