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

The*_*ght 3 c# regex xml asp.net match

我的输入文字是一击:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">2</string>
Run Code Online (Sandbox Code Playgroud)

用什么正则表达式模式从上面的输入中提取数字?

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

谢谢,

Ode*_*ded 5

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

"<string[^>]+>([0-9]+)</string>"
Run Code Online (Sandbox Code Playgroud)

分解:

<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>
Run Code Online (Sandbox Code Playgroud)

如果示例是整个字符串,您可能希望分别使用^$在开始和结束时锚定它.

注意我正在使用[0-9]而不是\d,因为在.NET \d中将匹配任何Unicode数字.