我有这个字符串文本:
<meta http-equiv="Content-Type" content="text/html;" charset="utf-8">
<style type="text/css">
body {
font-family: Helvetica, arial, sans-serif;
font-size: 16px;
}
h2 {
color: #e2703b;
}.newsimage{
margin-bottom:10px;
}.date{
text-align:right;font-size:35px;
}
</style>
Run Code Online (Sandbox Code Playgroud)
为了清晰起见,添加了换行符和标识符,实际字符串没有它
我怎样才能获得h2颜色的价值?在这种情况下它应该是 - 在这种情况下#e2703b;我不知道如何使用正则表达式.
更新 如果我这样尝试:
Match match = Regex.Match(cssSettings, @"h2 {color: (#[\d|[a-f]]{6};)");
if (match.Success)
{
string key = match.Groups[1].Value;
}
Run Code Online (Sandbox Code Playgroud)
它根本不起作用
我不确定正则表达式是否可行,但您可以使用此正则表达式提取值:
h2 \\{color: (#(\\d|[a-f]){6};)}
Run Code Online (Sandbox Code Playgroud)
从中获取第一个组将获得属于h2颜色的值.
编辑
这段代码应该得到它:
String regex = "h2 \\{color: (#(\\d|[a-f]){6};)}";
String input = "<meta http-equiv=\"Content-Type\" content=\"text/html;\" charset=\"utf-8\"><style type=\"text/css\">body {font-family: Helvetica, arial, sans-serif;font-size: 16px;}h2 {color: #e2703b;}.newsimage{margin-bottom:10px;}.date{text-align:right;font-size:35px;}</style>";
MatchCollection coll = Regex.Matches(input, regex);
String result = coll[0].Groups[1].Value;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28651 次 |
| 最近记录: |