gpt4 book ai didi

C# 正则表达式转义某些字符

转载 作者:太空宇宙 更新时间:2023-11-03 11:20:42 25 4
gpt4 key购买 nike

如何使用 C# 正则表达式转义字符串中的某些字符?

This is a test for % and ' thing? -> This is a test for \% and \' thing?

最佳答案

resultString = Regex.Replace(subjectString, 
@"(?<! # Match a position before which there is no
(?<!\\) # odd number of backlashes
\\ # (it's odd if there is one backslash,
(?:\\\\)* # followed by an even number of backslashes)
)
(?=[%']) # and which is followed by a % or a '",
@"\", RegexOptions.IgnorePatternWhitespace);

但是,如果您试图保护自己免受恶意 SQL 查询的侵害,则正则表达式不是正确的方法。

关于C# 正则表达式转义某些字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11091122/

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