3

I simply want to go through and find every numerical value in a single, or batch of, CSS files and multiple times two, then save.

Any suggestions for the easiest way to do this?

Xavier Ho
  • 17,011
  • 9
  • 48
  • 52
Someone
  • 85
  • 1
  • 7
  • 3
    Be prepared for a lot of unexpected results. – Blazemonger Nov 08 '11 at 18:36
  • 2
    What are you trying to accomplish? – Jason Gennaro Nov 08 '11 at 18:46
  • 2
    I don't have a solution to your problem, but to avoid these issues in the future, you can look at using extended stylesheet langues like SASS or LESS. – Casey Yee Nov 08 '11 at 18:48
  • That's a really bizarre question. I guess you can just read a token, check if it's a number and multiply it by two, and write to a file. – Xavier Ho Nov 09 '11 at 06:19
  • find all numeric value? you mean the numbers? – GaryNg Nov 09 '11 at 11:49
  • Deceptively simple question, no simple answer. For each line in every file you grab, you'll have to parse characters until you get to a number, then read it and all consecutive digits into a variable until you get to a non-digit, then multiply that whole number (assuming there's no decimal) by 2, replace the number with the doubled value. With that done, you'll then have to repeat the search until done. How to code this I don't know, but that's the process. Good luck. – Lizz Mar 01 '13 at 02:47

1 Answers1

1

Using regular expressions could solve your problem. For example, in python you could do the following:

import re

input = "#content {width:100px;height:20.5%;font-size:150.25%;margin-left:-20px;padding:2 0 -20 14.33333;}"

regex = re.compile("-?[.0-9]+")
scaled_numbers = [float(n)*2 for n in re.findall(regex, input)]
split_text = re.split(regex, input)

output = ''
for i in range(len(scaled_numbers)):
    output += "%s%.2f" % (split_text[i], scaled_numbers[i])

output += split_text[-1]

This code could be reduced in length, but I've deliberately left it less compact for readability. One flaw with it is that it contracts floats to only 2 decimal places, but that can be easily changed if you really need extended decimals (change the number in "%s%.2f" to the desired number of places).

Note also that this code could change the names of CSS selectors (for example, #footer-2 would become #footer-4.00). If you wanted to avoid that, you'll need to adjust the code to ignore text outside of {...}.

ASGM
  • 11,051
  • 1
  • 32
  • 53