英文:
Replace three dashes, but only if significant characters follow in the same line
问题
Sure, here's the translated portion of your request:
在多行文本中,我想将三个破折号 ---
替换为 X
。需要确切地连续出现三个破折号,而不是两个或四个。另一个限制条件是:如果 ---
是一行中的最后一个字符(除了可能的尾随空白字符),则不要替换它。
到目前为止,我得到了以下代码:
import re
text1 = "a---b"
text2 = "a\n --- \nb"
for text in [text1, text2]:
print(repr(re.sub("(?<!-)---(?!-)", "X", text)))
输出如下:
'aXb'
'a\n X \nb'
使用了负向预查/负向回顾。但我想要的是:
'aXb'
'a\n --- \nb'
如何实现规则,要求重要字符需要紧跟在 ---
后面呢?
(Note: I've translated the code part, as per your request, but please let me know if you need any additional information or assistance.)
英文:
In a multiline text, I would like to replace triple dashes, ---
, by X
. There need to be exactly three dashed in a row, not two or four. Another restriction: If ---
is the last character in the line (except possible trailing whitespace), don't replace it.
So far, I got
import re
text1 = "a---b"
text2 = "a\n --- \nb"
for text in [text1, text2]:
print(repr(re.sub("(?<!-)---(?!-)", "X", text)))
'aXb'
'a\n X \nb'
with negative lookahead/lookbehind. I would like to have
'aXb'
'a\n --- \nb'
though. How to I implement the rule that significant characters need follow the ---
?
答案1
得分: 2
以下是翻译好的部分:
You can assert that to the left are optional whitespace chars without newlines followed by a non whitespace char that is not a hyphen
(?<!-)---(?=[^\S\n]*[^-\s])
for text in [text1, text2]:
print(repr(re.sub(r"(?<!-)---(?=[^\S\n]*[^-\s])", "X", text)))
Output
'aXb'
'a\n --- \nb'
<hr>
Or you can assert not trailing spaces without newlines till the end of the string:
(?<!-)---(?!-|[^\S\n]*$)
for text in [text1, text2]:
print(repr(re.sub(r"(?m)(?<!-)---(?=[^\S\n]*[^-\s])", "X", text)))
Output
'aXb'
'a\n --- \nb'
英文:
You can assert that to the left are optional whitespace chars without newlines followed by a non whitespace char that is not a hyphen
(?<!-)---(?=[^\S\n]*[^-\s])
for text in [text1, text2]:
print(repr(re.sub(r"(?<!-)---(?=[^\S\n]*[^-\s])", "X", text)))
Output
'aXb'
'a\n --- \nb'
<hr>
Or you can assert not trailing spaces without newlines till the end of the string:
(?<!-)---(?!-|[^\S\n]*$)
for text in [text1, text2]:
print(repr(re.sub(r"(?m)(?<!-)---(?=[^\S\n]*[^-\s])", "X", text)))
Output
'aXb'
'a\n --- \nb'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论