[英]Python regex re.sub substitution to as desired
Using Python 3 consider this regex:使用 Python 3 考虑这个正则表达式:
[\||JI|\[|\]NL\||JI|\[|\]]
This will be deployed against a string such as:这将针对一个字符串进行部署,例如:
INLI VERONICA HD / DISNEY XD
Python used: Python 使用:
import re
new_string = re.sub("[\||JI|\[|\]NL\||JI|\[|\]]", "", old_string)
print(new_string)
Expected output:预期 output:
VERONICA HD / DISNEY XD
Actual output:实际 output:
NL VEROCA HD / DSEY XD
To ad some context, NL
could be wrapped in any combination of the characters J
, I
, |
为了说明一些上下文,
NL
可以包含在字符J
、 I
、 |
的任意组合中。 , [
and ]
, hence the either or options supplied in the regex. ,
[
和]
,因此正则表达式中提供了 or 或选项。 Anyone suggest how to amend to get the desired output?有人建议如何修改以获得所需的 output?
Thanks谢谢
You can use您可以使用
[][|JI]*NL[][|JI]*
See the regex demo .请参阅正则表达式演示。 Details :
详情:
[][|JI]*
- zero or more ]
, [
, |
[][|JI]*
- 零个或多个]
, [
, |
, J
or I
chars J
或I
字符NL
- NL
string NL
- NL
字符串[][|JI]*
- zero or more ]
, [
, |
[][|JI]*
- 零个或多个]
, [
, |
, J
or I
chars. J
或I
字符。 See the Python demo :请参阅Python 演示:
import re
old_string = "INLI VERONICA HD / DISNEY XD"
new_string = re.sub(r"[][|JI]*NL[][|JI]*", "", old_string).strip()
print(new_string)
# => VERONICA HD / DISNEY XD
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.