Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我想去掉除数字、$、逗号(,)以外的所有内容_C#_Regex - Fatal编程技术网

C# 我想去掉除数字、$、逗号(,)以外的所有内容

C# 我想去掉除数字、$、逗号(,)以外的所有内容,c#,regex,C#,Regex,我想去掉除数字、$、逗号(,)以外的所有内容 这只是脱衣舞字母 string Cadena; Cadena = tbpatronpos6.Text; Cadena = Regex.Replace(Cadena, "([^0-9]|\\$|,)", ""); tbpatronpos6.Text = Cadena; 为什么我的正则表达式不起作用,我如何修复它?你想要这样的东西吗 [^\\d\\$,] 我怀疑这就是你想要的: usi

我想去掉除数字、$、逗号(,)以外的所有内容

这只是脱衣舞字母

        string Cadena;
        Cadena = tbpatronpos6.Text;

        Cadena = Regex.Replace(Cadena, "([^0-9]|\\$|,)", "");
        tbpatronpos6.Text = Cadena;

为什么我的正则表达式不起作用,我如何修复它?

你想要这样的东西吗

[^\\d\\$,]

我怀疑这就是你想要的:

using System;
using System.Text.RegularExpressions;

class Test
{
    static void Main(string[] args)
    {
        string original = @"abc%^&123$\|a,sd";
        string replaced = Regex.Replace(original, @"[^0-9$,]", "");
        Console.WriteLine(replaced); // Prints 123$,
    }
}
问题是你使用了交替运算符,基本上-你只需要对所有的(数字、逗号、美元)进行设置否定


请注意,您不需要在字符组中转义美元。

“请注意,您不需要在字符集中转义美元。”-您确定这是正确的吗?IIRC这只是一个字符集中被认为是其实际值的最后一个字符,其余的字符需要转义?@user551841:嗯,我给出的代码对我来说很有用。它可能是一个特定于.NET的东西,但我不明白如果它被解释为锚,为什么它会打印美元。