正则表达式删除python中的特定单词 [英] Regex to remove specific words in python
问题描述
我想在 python 中使用正则表达式进行一些操作.
I want to do the some manipulation using regex in python.
所以输入是 +1223,+12_remove_me,+222,+2223_remove_me
和输出应该是 +1223,+222
So input is +1223,+12_remove_me,+222,+2223_remove_me
and
output should be +1223,+222
输出应该只包含逗号分隔的单词,不包含 _remove_me
并且每个单词之间只有一个逗号.
Output should only contain comma seperated words which don't contain _remove_me
and only one comma between each word.
注意:我尝试过的正则表达式 \+([0-9|+]*)_
, \+([0-9|+]*)
和其他一些我没有得到所需输出的组合.
Note: REGEX which I tried \+([0-9|+]*)_
, \+([0-9|+]*)
and some other combination using which I did not get required output.
注意 2 我不能使用循环,需要在没有循环的情况下只使用正则表达式.
Note 2 I can't use loop, need to do that without loop with regex only.
推荐答案
您的正则表达式似乎不完整,但您走对了.请注意,字符类中的管道符号被视为文字,您的 [0-9|+]
匹配数字或 |
或 +
符号.
Your regex seems incomplete, but you were on the right track. Note that a pipe symbol inside a character class is treated as a literal and your [0-9|+]
matches a digit or a |
or a +
symbols.
你可以使用
,?\+\d+_[^,]+
查看正则表达式演示
说明:
,?
- 可选的,
(如果单词"在字符串的开头,则应该是可选的)\+
- 文字+
\d+
- 1+ 个数字_
- 文字下划线[^,]+
- 除了,
之外的 1+ 个字符
,?
- optional,
(if the "word" is at the beginning of the string, it should be optional)\+
- a literal+
\d+
- 1+ digits_
- a literal underscore[^,]+
- 1+ characters other than,
import re
p = re.compile(r',?\+\d+_[^,]+')
test_str = "+1223,+12_remove_me,+222,+2223_remove_me"
result = p.sub("", test_str)
print(result)
# => +1223,+222
这篇关于正则表达式删除python中的特定单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!