[英]Python Regex: replace multiple possibilities of substring
我想在字符串caption
中删除Fig 1.
的指示器,其中caption
可能是:
# each line is one instance of caption
"Figure 1: Path of Reading Materials from the Web to a Student."
"FIGURE 1 - Travel CP-net"
"Figure 1 Interpretation as abduction, the big picture."
"Fig. 1. The feature vector components"
"Fig 1: IMAGACT Log-in Page"
"FIG 1 ; The effect of descriptive and interpretive information, and Inclination o f Fit"
...
我试过caption = re.sub(r'figure 1: |fig. 1 |figure 1 -', '', caption, flags=re.IGNORECASE)
,但看起来很乱:我真的需要列出所有手动的可能性? 是否有任何元素重新编码来匹配它们?
非常感谢!
您可以使用可选部分来匹配ure
并使用可选字符 class 来匹配:
, .
, ;
或-
如果要匹配 1 以外的其他数字,请使用\d+
\bfig\.?(?:ure)? 1[^\S\r\n]*[:.;–-]?
\bfig
匹配前面有单词边界的 fig\.?
匹配一个可选的点(?:ure)?
可选ure
1
匹配一个空格和1
[^\S\r\n]*
匹配 0+ 次出现的空白字符,换行符除外[:.;–-]?
可选匹配字符 class 中列出的任何一个示例代码也匹配字符 class 之后的空格:
caption = re.sub(r'\bfig\.?(?:ure)? 1[^\S\r\n]*[:.;–-]?[^\S\r\n]', '', caption, flags=re.IGNORECASE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.