【问题标题】:Replacing only the matched group in a file with multiple occurences仅替换文件中多次出现的匹配组
【发布时间】:2021-01-10 18:58:53
【问题描述】:

输入: /* ABCD X 1111 */ /* Comment 1111: [[reason for comment]] */

输出: /* ABCD X 1111 # [[reason for comment]] */

使用的正则表达式regex = (?:[\/*]+\sPRQA[\s\w\,]*)(\*\/\s*\/\*\Comment[\w\,]+:)+(?:\s\[\[.*\/$)

如何使用上述正则表达式将匹配的组替换为'#'在文件中多次出现?

我尝试使用 re.sub(regex, '#\1', file.read(), re.MULTILINE),但这会将 # 附加到匹配的组。

有没有直接的方法来代替逐行迭代然后替换?

【问题讨论】:

    标签: python python-3.x regex regex-group regexp-replace


    【解决方案1】:

    你可以使用

    re.sub(r'(/\*\s*ABCD[^*/]*)\*/\s*/\*\s*Comment[^*:]+:(\s*\[\[[^][]*]]\s*\*/)', r'\1#\2', file.read())
    

    如果您确定这些子字符串仅出现在行尾,请添加您的 $ 锚点并使用 flags=re.M

    re.sub(r'(/\*\s*ABCD[^*/]*)\*/\s*/\*\s*Comment[^*:]+:(\s*\[\[[^][]*]]\s*\*/)$', r'\1#\2', file.read(), flags=re.M)
    

    请参阅regex demo详情

    • (/\*\s*ABCD[^*/]*) - 第 1 组 (\1):/*,零个或多个空格,ABCD,然后是除 */ 之外的任何零个或多个字符
    • \*/\s*/\*\s*Comment[^*:]+: - */,零个或多个空格,/,零个或多个空格,Comment,一个或多个除*: 之外的字符,然后是:
    • (\s*\[\[[^][]*]]\s*\*/) - 第 2 组 (\2):零个或多个空格,[[,除 []] 之外的零个或多个字符,]],零个或多个空格,*/。李>

    Python demo:

    import re
    rx = r'(/\*\s*ABCD[^*/]*)\*/\s*/\*\s*Comment[^*:]+:(\s*\[\[[^][]*]]\s*\*/)$'
    text = "Some text ... /* ABCD X 1111 */ /* Comment 1111: [[reason for comment]] */\nMore text here... Some text ... /* ABCD XD 1222 */ /* Comment 1112: [[reason for comment 2]] */"
    print( re.sub(rx, r'\1#\2', text, flags=re.M) )
    

    输出:

    Some text ... /* ABCD X 1111 # [[reason for comment]] */
    More text here... Some text ... /* ABCD XD 1222 # [[reason for comment 2]] */
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-08
      • 2022-12-04
      • 1970-01-01
      • 1970-01-01
      • 2019-08-24
      • 1970-01-01
      • 2012-02-27
      • 2010-09-21
      相关资源
      最近更新 更多