Reputation: 5386
I am trying to write a method in c# to convert PX to EM and viceversa in a css file.
To accomplish that i want to use regex to match the value (eg 12em) and convert it into px.
So far i have the following expression
(.*)^([0-9,-\.]+)(px)(.*)
this matches
border: solid 3em gray;
margin: 1.2em 2em 3em 4em;
margin: 1em 2em 3em 4em ;
but not
width:50.1em;
width: 50.1em;
Any suggestion?
Upvotes: 1
Views: 294
Reputation: 5386
The pattern that worked for me is
(.*?:?)([0-9,-\.]+)\s*(em)(.*)
Thanks alextercete for pointing to that Regular Expression Helper.
Upvotes: 0
Reputation: 86270
You could use a much simpler needle, like this one.
([\d.])+em
There are probably potential cases where it wouldn't work, but they are few and far between, e.g.
background-image: url('http://site.tld/something/5em/not/likely/');
If you need prevention of something like that, get a CSS parser.
Upvotes: 2