繁体   English   中英

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

[英]What regex pattern to use to extract the number from an xml input?

我的输入文字是一击:

<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);

谢谢,

这种模式应该可以解决问题:

"<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数字。

另一个使用LinqToXml的方法:

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

更新

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

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

这是非正则表达方式。

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);

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

    /// <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;
    }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM