C# 如何使用正则表达式从字符串中获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10798699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:18:10  来源:igfitidea点击:

how to use regular expression to get value from string

c#regex

提问by revolutionkpi

I have this string text:

我有这个字符串文本:

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

Newlines and idents are added for clarity, real string does not have it

为清晰起见添加了换行符和标识符,实际字符串没有

How can I get value of h2color? In this case it should be - #e2703b;I don't know how to use regular expressions in this case.

我怎样才能获得h2颜色的价值?在这种情况下应该是 -#e2703b;我不知道在这种情况下如何使用正则表达式。

UpdateIf I try this way:

更新如果我尝试这种方式:

Match match = Regex.Match(cssSettings, @"h2 {color: (#[\d|[a-f]]{6};)");
                    if (match.Success)
                    {
                        string key = match.Groups[1].Value;
                    }

it doesn't work at all

它根本不起作用

采纳答案by Terry

I'm not sure if regex is the way to go, but you can extract the value by using this regex:

我不确定正则表达式是否可行,但您可以使用此正则表达式提取值:

h2 \{color: (#(\d|[a-f]){6};)}

Getting the first Group from this will get you the value that belongs to the color of the h2.

从中获取第一个 Group 将获得属于 h2 颜色的值。

Edit

编辑

This piece of code should get it:

这段代码应该得到它:

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;

回答by gout

As you have said there are no tabs[\s] and line feeds[\n]in the string.Hence the regular expression would be:

正如您所说,字符串中没有制表符[\s] 和换行符[\n]。因此正则表达式为:

(?<=[.]*h2{color:)[#\w]*(?=[.]*)

Hence the code becomes,

因此代码变为,

Match match = Regex.Match(cssSettings, @"(?<=[.]*h2{color:)[#\w]*(?=[.]*)");
                if (match.Success)
                {
                    string key = match.Value;
                }

回答by CB.

Try this:

尝试这个:

@"h2\s*{\s*color: (#.{6};)"

回答by primfaktor

This should be quite robust wrt. optional spaces, line breaks and stuff. Also finds half-width color codes and does not jump into the next block if h2has no color.

这应该是相当健壮的。可选空格、换行符等。还发现半宽色码,并且如果不跳到下一个块h2不具有color

h2\s*\{[^}]*color\s*:\s*?(#[a-f\d]{3}|#[a-f\d]{6})\b

Result in the first and only captured group.

结果是第一个也是唯一一个被捕获的组。

Try it!

尝试一下!