c# - 正则表达式选择子字符串的模式

标签 c# regex

在 Regex C# 中查找和选择字符串的一部分的语法是什么?

字符串可能是:

string tdInnerHtml = "<strong> You gained  230 Points </strong> 
                      there is going to be more text and some html code part of this       
                      string <a href=http://google.com>Google it here </a>";

// I want to extract 230 from this string using Regx. 
// The digits (230) vary for each tdInnerHtml. 
// So code would be to look for digits, followed by space, ending with Points

最佳答案

如果空格和 </strong>标签是一致的,您可以使用以下内容来获得匹配,并且可以使用以下字符串:“品脱在 230-240 点之间,您获得了 230 点

        var match = Regex.Match(tdInnerHtml, @"(?<pts>\d+) Points ?</strong>");
        if (match.Success) {
            int points = Convert.ToInt32(match.Groups["pts"].Value);
            Console.WriteLine("Points: {0}", points);
        }

关于c# - 正则表达式选择子字符串的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9999544/

相关文章:

c# - C# 应用程序和非托管 C++ 应用程序之间的进程间通信

mysql - 将大量扩展插入转换为 mysql 的简单插入

Java方法在字符串中查找字母集不特定于字母序列?

regex - 使用正则表达式替换字符串中的换行符

python - 如何从网址中提取标题?

javascript - 在js中用多个定界符和特殊字符分割

c# - 没有依赖关系的简单字符串加密

c# - 调用Web Services时HTTP状态405 "Method Not Allowed"如何解决

c# - 对事件使用通用的清除文本框方法

c# - ASP.NET 变量的 JavaScript 语法错误