fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.util.regex.*;
  5. import java.lang.*;
  6. import java.io.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. String imgStyle = "width: 300px; height: 295px;";
  14.  
  15. int imgHeight = 0;
  16. int imgWidth = 0;
  17.  
  18. Pattern h = Pattern.compile("height:\\s+(\\d+)px;");
  19. Pattern w = Pattern.compile("width:\\s+(\\d+)px;");
  20.  
  21. Matcher m1 = h.matcher(imgStyle);
  22. Matcher m2 = w.matcher(imgStyle);
  23.  
  24. if (m1.find()) {
  25. imgHeight = Integer.parseInt(m1.group(1));
  26. }
  27.  
  28. if (m2.find()) {
  29. imgWidth = Integer.parseInt(m2.group(1));
  30. }
  31.  
  32. System.out.println("Width is: "+imgWidth);
  33. System.out.println("Height is: "+imgHeight);
  34. }
  35. }
Success #stdin #stdout 0.1s 320320KB
stdin
Standard input is empty
stdout
Width is: 300
Height is: 295