【发布时间】: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