GigaPr
GigaPr

Reputation: 5386

Regex to match em in css file

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

Answers (4)

GigaPr
GigaPr

Reputation: 5386

The pattern that worked for me is

(.*?:?)([0-9,-\.]+)\s*(em)(.*)

Thanks alextercete for pointing to that Regular Expression Helper.

Upvotes: 0

Brigand
Brigand

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.

demo

Upvotes: 2

alextercete
alextercete

Reputation: 5171

Maybe: ([0-9,-\.]+)\s*(em)? See the example.

Upvotes: 0

grifos
grifos

Reputation: 3381

Try (.+):(.*)^([0-9,-\.]+)(px)(.*)

Upvotes: 0

Related Questions