Java tutorial
//package com.java2s; import org.w3c.dom.*; import java.util.regex.*; public class Main { static final Matcher fpMatch = Pattern .compile("([-+]?((\\d*\\.\\d+)|(\\d+))([eE][+-]?\\d+)?)(\\%|in|cm|mm|pt|pc|px|em|ex)?").matcher(""); /** * Parses the given attribute of this tag and returns it as a float */ public static float getAttribFloat(Element ele, String name) { String sval = ele.getAttribute(name); float val = 0.0f; try { val = Float.parseFloat(sval); } catch (Exception e) { } return val; } public static float parseFloat(String val) { /* if (val == null) return 0f; float retVal = 0f; try { retVal = Float.parseFloat(val); } catch (Exception e) {} return retVal; */ return findFloat(val); } /** * Searches the given string for the first floating point number it contains, * parses and returns it. */ public synchronized static float findFloat(String val) { if (val == null) return 0f; fpMatch.reset(val); if (!fpMatch.find()) return 0f; val = fpMatch.group(1); //System.err.println("Parsing " + val); float retVal = 0f; try { retVal = Float.parseFloat(val); String units = fpMatch.group(6); if ("%".equals(units)) retVal /= 100; } catch (Exception e) { } return retVal; } }