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
Post a Comment