[英]Remove escape characters from string using c#
string str = @"*[@Name=\'Cd.exe\' and @ControlType=\'ControlType.Button\' and @AutomationId=\'C:\bin\Sp.exe\']";
var output = System.Text.RegularExpressions.Regex.Unescape(str);
我想從上面的字符串中刪除轉義字符,但上面的字符串也包含FilePath所以我應該如何處理它
上面的代碼在下面拋出異常
ArgumentOutOfRange Exception
parsing "*[@Name=\'Cd.exe\' and @ControlType=\'ControlType.Button\' and @AutomationId=\'C:\bin\Sp.exe\']" - Unrecognized escape sequence \S.
您的字符串無效。 如果應該包含轉義的正則表達式字符,那么文件路徑也必須轉義。 例如:
@AutomationId='C:\\bin\\Sp\\.exe'
必須轉義反斜杠\
,以避免將\S
解釋為特殊字符。 點.
也必須逃脫。
此外,正則表達式語法中不會轉義單引號'
字符: Character Escapes in .NET
在我看來,您正在嘗試使用正則表達式 unescape 取消轉義 JavaScript 字符串? 那是行不通的。
很可能你只需要這個:
string str = @"*[@Name=\'Cd.exe\' and @ControlType=\'ControlType.Button\' and @AutomationId=\'C:\bin\Sp.exe\']";
var output = str.Replace("\\'", "'");
System.Diagnostics.Debug.WriteLine(output);
// Debug output:
*[@Name='Cd.exe' and @ControlType='ControlType.Button' and @AutomationId='C:\bin\Sp.exe']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.