2

我在这里有这段代码,我需要返回只有数字的args.Content (我的输入数据)并删除其余的字符。我一直在用正则表达式尝试很多东西,但它对我不起作用。我对 C# 几乎一无所知,我真的需要这个网站的程序员的帮助。

using System;
using VisualWebRipper.Internal.SimpleHtmlParser;
using VisualWebRipper;
public class Script
{

    public static string TransformContent(WrContentTransformationArguments args)
    {
        try
        {
            //Place your transformation code here.
            //This example just returns the input data
            return args.Content;
        }
        catch(Exception exp)
        {
            //Place error handling here
            args.WriteDebug("Custom script error: " + exp.Message);
            return "Custom script error";
        }
    }
}

希望你能帮忙

4

3 回答 3

3

只需删除任何不是数字的内容。有一个预定义的数字字符类:\d,否定是\D

所以你的正则表达式很简单:

\D+

在您的 C# 代码中,它类似于

return Regex.Replace(args.Content, @"\D+", "");
于 2012-05-25T07:45:50.343 回答
2

当然不是最有效的,但是哦,好吧,我忍不住做了一些 LINQ:

var digitsOnly = new string(args.Content.Where(c => char.IsDigit(c)).ToArray())
于 2012-05-25T07:48:37.663 回答
0
StringBuilder builder = new StringBuilder();
Regex regex = new Regex(@"\d{1}");
MatchCollection matches = regex.Matches(args.Content);
foreach (var match in matches)
{
    builder.Append(match.ToString());
}
return builder.ToString();
于 2012-05-25T07:52:19.210 回答