I have a CSS parser utility written in C#. I am able to parse and extract all CSS classes using following regex. This is working as intended.
[C#]
const string expression = "(.*?)\\{(.*?)\\}";
var regEx = new Regex(expression, RegexOptions.Singleline | RegexOptions.IgnoreCase);
var matches = regEx.Matches(styleSheet);
[CSS]
body
{
font-family: Helvetica Neue,Helvetica,Arial,sans-serif;
font-size: 13px;
color: #666666;
}
img
{
border: 0;
display: block;
}
@media only screen and (max-width: 600px)
{
table[class=bodyTable]
{
width: 100% !important;
}
table[class=headerlinks]
{
display:none !important;
}
}
a
{
text-decoration: none;
}
However now our software have started supporting media queries and for some reason we want to ignore whole media queries during CSS parsing. So it should only match body, img and a.
Appreciate if someone can help me with writting new regex :)
[Workaround] Once I get all matches, in my code I have to do some processing by using foreach -
foreach(Match match in matches)
{
var selectorString = match.Groups[1].ToString();
if (selectorString.IndexOf("@media", StringComparison.InvariantCulture) > -1)
continue;
// processing...
}