开发者

edit delimited string with regular expression

开发者 https://www.devze.com 2022-12-19 08:50 出处:网络
i have a string which i want to edit a part of it. the string is like \"开发者_开发知识库1:5,7:9,13:20,130:510,134:2,\"

i have a string which i want to edit a part of it. the string is like

"开发者_开发知识库1:5,7:9,13:20,130:510,134:2,"

now all i want to do is remove the first part of those numbers like

"5,9,20,540,2,"

i tried a bunch of combinations but didn't get what i expected.

Regex rx = new Regex("[:]\\d+[,]");
    foreach (Match mx in rx.Matches("10:20,20:30,"))
    {
        Muhaha.InnerText += mx;
    }

it returns ":20,:30," but i want to capture only the number, nut the punctuation.


How about using a Replace instead?

Regex r = new Regex("\\d+:");
string str = r.Replace("1:5,7:9,13:20,130:510,134:2,", "");
Console.WriteLine(str);

Prints:

5,9,20,510,2,


Try this, if you want to manipulate that numbers before joining them (if not, you should go with @Aistina answer):

foreach(Match m in Regex.Matches(
    "1:5,7:9,13:20,130:510,134:2,", 
    @":(?'number'\d+)"))
{
    Console.WriteLine(m.Groups["number"].Value);
}
0

精彩评论

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