string str = @"*[@Name='Cd.exe' and @ControlType='ControlType.Button' and @AutomationId='C:binSp.exe']";
var output = System.Text.RegularExpressions.Regex.Unescape(str);
我想从上面的字符串中删除转义字符,但在上面的字符串中也包含文件路径那么我应该如何处理它
上面的代码在下面抛出异常
ArgumentOutOfRange Exception
parsing "*[@Name='Cd.exe' and @ControlType='ControlType.Button' and @AutomationId='C:binSp.exe']" - Unrecognized escape sequence S.
您的字符串无效。如果 is 应该包含转义的正则表达式字符,则文件路径也必须转义。例如:
@AutomationId='C:\bin\Sp\.exe'
反斜杠必须转义,以避免将
S
解释为特殊字符。点.
也必须被转义。
此外,正则表达式语法中不会转义单引号'
字符:.NET 中的字符转义
在我看来,您正在尝试使用正则表达式取消转义 JavaScript 字符串?那行不通。
很可能你只需要这个:
string str = @"*[@Name='Cd.exe' and @ControlType='ControlType.Button' and @AutomationId='C:binSp.exe']";
var output = str.Replace("\'", "'");
System.Diagnostics.Debug.WriteLine(output);
// Debug output:
*[@Name='Cd.exe' and @ControlType='ControlType.Button' and @AutomationId='C:binSp.exe']