. Topicala Page Index Token

A Journal about the experiences I have developing little applications in C#, Perl, Html and Javascript and talking about things new things that I use. Always Geeky; Always Nerdy; Always poor Grammer!

I am a Software Analyst Developer working in Southport, England but living in Liverpool. I develop mainly in C# and ASP.Net. I have been developing comercial software for several years now. I maintain this site (hosted at SwitchMedia UK) as a way of exploring new technologies (such as AJAX) and just generally talking about techie geek issues. This site is developed through a host of Perl scripts and a liberal use of Javascript. I enjoy experimenting with new technologies and anything that I make I host here.

Quick Search

Web www.kinlan.co.uk

Monday, February 06, 2006

Answer to CSS Regex Question

I have had a question in my Comments by Rasmus about CSS Regex for obtaining CSS class names. And rather than post it in the comments, I will post it as abn entry just so everyone can see it.

The Regex I gave was
\.[-]?[_a-zA-Z][_a-zA-Z0-9-]*|[^\0-\177]*\\[0-9a-f]{1,6}(\r\n[ \n\r\t\f])?|\\[^\n\r\f0-9a-f]*

I think this is nearly correct and the C# that it is not supposed to work with is:


string css = @"
.class1{
color:pink;
}
.class2{
color:blue;
}";
string patt = @"\.[-]?[_a-zA-Z][_a-zA-Z0-9-]*[^\0-\177]*\\[0-9a-f]{1,6}(\r\n[ \n\r\t\f])?\\[^\n\r\f0-9a-f]*";
string[] arr = Regex.Split(css, patt);
}

I tried the above code and it worked as Expected, there is an array entry in arr at the index of each class. Therefore each entry in arr contains the text between the class names.

Try Regex.Match if you want to get at the names of the classes. And Check that the | character comes out correctly.

Technorati Tags
[feed], [feed], [feed], [feed], [feed], [feed]

Related Wikipedia Documents
, , ,

My Related Documents
,

Related Amazon Books
The ZEN of CSS Design: Visual Enlightenment for the Web: / CSS Pocket Reference: / Professional CSS: / Eric Meyer on CSS: /

Related Images From Flickr

Comments: [Add New]

Thanks for the update!

It's exactly the class names i'm interested in. Using the Regex.Match method helps.

But in this sample only the first class is returned.


static void Main(string[] args) {
string css = @"
.class1{
color:pink;
}
.class2{
color:blue;
}";
string patt = @"\.[-]?[_a-zA-Z][_a-zA-Z0-9-]*|[^\0-\177]*\\[0-9a-f]{1,6}(\r\n[ \n\r\t\f])?|\\[^\n\r\f0-9a-f]*";
Match match = Regex.Match(css, patt);

if (match.Captures.Count != 2) {
//failed to find both
}
}

By Rasmus, at Monday, February 06, 2006 2:59:00 PM

I made a mistake with what I told you. Sorry :)

check out the new answer

By Paul Kinlan, at Monday, February 06, 2006 3:38:00 PM