3

我的输入文本是打击:

<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

4 回答 4

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]and not \d,因为在 .NET\d中将匹配任何 Unicode 数字。

于 2012-12-13T11:13:40.507 回答
2

使用 LinqToXml 的另一种方法:

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

更新

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

(?<=<string[^>]+>)([0-9]+)(?=</string>)
于 2012-12-13T11:41:05.493 回答
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);
于 2012-12-13T11:19:19.007 回答
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;
    }

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

于 2012-12-13T14:16:33.727 回答