开发者

I want to strip off everything but numbers, $, comma(,)

开发者 https://www.devze.com 2023-04-07 23:08 出处:网络
I want to strip off everything but numbers, $, comma(,). this only strip letters string Cadena; Cadena = tbpatronpos6.Text;

I want to strip off everything but numbers, $, comma(,).

this only strip letters

        string Cadena;
        Cadena = tbpatronpos6.Text;

        Cadena = Regex.Replace(Cade开发者_开发问答na, "([^0-9]|\\$|,)", "");
        tbpatronpos6.Text = Cadena;

Why doesn't my regex work, and how can I fix it?


I suspect this is what you want:

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$,
    }
}

The problem was your use of the alternation operator, basically - you just want the set negation for all of (digits, comma, dollar).

Note that you don't need to escape the dollar within a character group.


you want something like this?

[^\\d\\$,]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号