用于从xml输入中提取数字的正则表达式模式是什么?

时间:2012-12-13 11:10:20

标签: c# asp.net xml regex match

我的输入文字就像打击一样:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">2</string>

用于从上述输入中提取数字的正则表达式模式?

var pattern = "<string ?>?</string>"; // how to write this?
var match = Regex.Match(input, pattern, RegexOptions.IgnoreCase);

谢谢,

4 个答案:

答案 0 :(得分:5)

这种模式可以解决问题:

"<string[^>]+>([0-9]+)</string>"

故障:

<string   - Match the string <string
[^>]+     - Followed by one or more characters that are not >
>         - Followed by >
(         - Start capturing group
[0-9]+    - Followed by one or more of the digits 0-9
)         - End capturing group
</string> - Followed by the string </string>

如果示例是整个字符串,您可能希望分别在开头和结尾使用^$来锚定它。

注意我使用[0-9]而非\d,因为.NET \d将匹配任何Unicode数字。

答案 1 :(得分:2)

使用LinqToXml的另一个方法:

var ele = XElement.Parse("<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">2</string>");
var valueString = ele.Value; //valueString = "2";

<强>更新

对于正则表达式:我会使用(?<=startRegex)(?=endRegex)(lookbehind和lookahead)从@Oded扩展解决方案,因此匹配值中将省略不必要的<string>标记。

(?<=<string[^>]+>)([0-9]+)(?=</string>)

答案 2 :(得分:1)

这是非正则表达方式。

string str = "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">2</string>";
int startIndex = str.IndexOf('>');
int endIndex = str.LastIndexOf('<');
int numberLenght =  (endIndex - startIndex) - 1;
string result = str.Substring(startIndex + 1, numberLenght);

答案 3 :(得分:1)

您可以使用此方法提取数字:

    /// <summary>
    /// Example for how to extract the number from an xml string.
    /// </summary>
    /// <param name="xml"></param>
    /// <returns></returns>
    private string ExtractNumber(string xml)
    {
        // Extracted number.
        string number = string.Empty;

        // Input text
        xml = @"<string xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">2</string>";

        // The regular expression for the match.
        // You can use the parentesis to isolate the desired number into a group match. "(\d+?)"
        var pattern = @"<string.*?>(\d+?)</string>";

        // Match the desired part of the xml.
        var match = Regex.Match(xml, pattern);

        // Verify if the match has sucess.
        if (match.Success)
        {
            // Finally, use the group value to isolate the number.
            number = match.Groups[1].Value;
        }

        return number;
    }

这是我用来解决这个问题的方法。