替换三个破折号,但仅当在同一行中后面跟随着重要字符时。

huangapple go评论71阅读模式
英文:

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 = &quot;a---b&quot;
text2 = &quot;a\n  --- \nb&quot;

for text in [text1, text2]:
    print(repr(re.sub(&quot;(?&lt;!-)---(?!-)&quot;, &quot;X&quot;, text)))
&#39;aXb&#39;
&#39;a\n  X \nb&#39;

with negative lookahead/lookbehind. I would like to have

&#39;aXb&#39;
&#39;a\n  --- \nb&#39;

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])

Regex demo

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]*$)

Regex demo

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

(?&lt;!-)---(?=[^\S\n]*[^-\s])

Regex demo

for text in [text1, text2]:
    print(repr(re.sub(r&quot;(?&lt;!-)---(?=[^\S\n]*[^-\s])&quot;, &quot;X&quot;, text)))

Output

&#39;aXb&#39;
&#39;a\n  --- \nb&#39;

<hr>

Or you can assert not trailing spaces without newlines till the end of the string:

(?&lt;!-)---(?!-|[^\S\n]*$)

Regex demo

for text in [text1, text2]:
    print(repr(re.sub(r&quot;(?m)(?&lt;!-)---(?=[^\S\n]*[^-\s])&quot;, &quot;X&quot;, text)))

Output

&#39;aXb&#39;
&#39;a\n  --- \nb&#39;

huangapple
  • 本文由 发表于 2023年5月10日 23:08:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220054.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定