gpt4 book ai didi

c# - 遇到无法识别的转义序列继续反转义

转载 作者:行者123 更新时间:2023-11-30 16:52:28 29 4
gpt4 key购买 nike

我有一个系统可以处理一些提供的数据。

在存储数据之前,我像这样取消转义字符:

Regex.Unescape(text);

我遇到了一堆 ArgumentException: <str> includes an unrecognized escape sequence因为一些数据包含如下文本: \m/\o/甚至 ¯\_(ツ)_/¯ .

有什么方法可以忽略无法识别的序列并继续对输入的其余部分进行转义吗?

最佳答案

当您的字符串来自未知来源时,您不能依赖 Regex.Unescape。查看MSDN reference :

Unescape cannot reverse an escaped string perfectly because it cannot deduce precisely which characters were escaped.

It reverses the transformation performed by the Escape method by removing the escape character ("\") from each character escaped by the method. These include the \, *, +, ?, |, {, [, (,), ^, $,., #, and white space characters. In addition, the Unescape method unescapes the closing bracket (]) and closing brace (}) characters.

It replaces the representation of unprintable characters with the characters themselves. For example, it replaces \a with \x07. The character representations it replaces are \a, \b, \e, \n, \r, \f, \t, and \v.

你可以像这样模拟Regex.Unescape

var unescaped = Regex.Replace(input, @"\\([\\*+?|{}[\]()^$. #])", "$1");

参见 regex demo

如果\*+?中有转义字符|, {, [, (,), ^ , $,., #, }] 设置,反斜杠将被移除。

关于c# - 遇到无法识别的转义序列继续反转义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32510510/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com