【发布时间】:2021-12-08 09:23:13
【问题描述】:
我有一个问题,例如我有一个文本:
input = "text, *text*, text, text, (text), [text], [text](http://.....) *text*, text, text, (text), [text]"
我正在尝试将字符 '_'、'*'、'['、']'、'('、')' 替换为 '\_'、'\*' 等。
我在写:
pattern = @"(?<!\[(?<text>.*)\]\((?<url>.*))([[\]\(\)*_])";
input = Regex.Replace(input, pattern, @"\$1");
System output: "text, \*text\*, text, text, \(text\), \[text\], \[some\_text with \_ \* \]\(http://.....) \*text\*, text, text, \(text\), \[text\]"
如何确保链接 []() 的设计不会改变?即它看起来像:
desired output:"text, \*text\*, text, text, \(text\), \[text\], [some\_text with \_ \*](http://.....) \*text\*, text, text, \(text\), \[text\]"
【问题讨论】:
-
您的正则表达式是否匹配您需要转义的所有字符?是否与Escaped character on Telegram Bot API 4.5 MarkdownV2 gives trouble for hyper link有关
-
@WiktorStribiżew,我没有写所有字符,因为我试图通过链接解决问题,但我不太明白如何解决。我试过: (? . *) ] \ ((?
. *)) ([| * {} # + = \ -_.! ~> `]),但是它仍然像这样工作:\ [... \] \ (url \) -
请在代码周围使用反引号格式化问题。目前尚不清楚什么是逃逸的,什么不是。检查我的示例编辑。
-
@WiktorStribiżew,我修正了格式,谢谢你的例子