正则表达式匹配字符串中非ascii字符\u0000-\u007F的字符串表示并替换为C#中的空字符串?

问题描述 投票:0回答:0

我在我的字符串中得到 unicode 字符的十六进制表示,并想用空字符串替换它。更具体地说,尝试使用正则表达式匹配字符串中 \u0000-\u007F 中的所有值,用 C# 将其替换为空字符串。

例一:

输入字符串:“\u007FTestString”

预期结果:测试字符串

例二:

输入字符串:“\u007FTestString\U0000”

预期结果:测试字符串

我目前的解决方案确实如此

            if (!string.IsNullOrWhiteSpace(testString))
            {
                return Regex.Replace(testString, @"[^\u0000-\u007F]", string.Empty);
            }

不匹配非ascii字符的十六进制表示。我如何让它与字符串中的 \u0000-\u007F 匹配?

任何帮助表示赞赏。谢谢!

c# regex unicode hex pattern-matching
© www.soinside.com 2019 - 2024. All rights reserved.