c# - Get ClassName from a css file -


i'd css-class names css file.

my css file looks this:

p.juicy{     margin-left:40px; }  p.csbody{     text-align:justify; }  p.cscode{     font-family:"lucida console", monaco, monospace;     background-color:silver;     padding: 20px;     text-align:left; } 

i have following code:

     string filename = system.io.file.readalltext(dir);          matchcollection mt = regex.matches(filename, @"[^}]?([^{]*{[^}]*})", regexoptions.multiline);         list<string> list = new list<string>();         (int = 0; < mt.count; i++)         {             string cls = mt[i].captures[0].tostring().trim();             var classname = cls.substring(1, cls.indexof("{") - 1).trim().replace(":before", "").replace(":after", "");              list.add(classname);          } 

i'm not familiar regex. there other work-around this?

thank you

regex if want strings out of regularly structured text quite poor when need "parse" stuff. in case, it'll fall down if file contains string literals happen contain css-selctor structures within them. example, have content: ".im-a-word-with-a-space-afterwards {". it'll have trouble if forget parse out pseudo selectors such .my-class:before , combinations .one-class.another-class.

if know for sure structure of css reading (e.g. because it's your css) i'd recommend sticking regex , bodging around edge cases. fine shorter jobs , internal one-off scripts.

if need super-robust solution getting class names i'd either write proper css parser (unwise) or available 1 such excss "understands" css enough extract out information class names.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -