fork(1) download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import java.util.regex.Pattern;
  5. import java.util.regex.Matcher;
  6.  
  7. class Ideone
  8. {
  9. public static void main (String[] args) throws java.lang.Exception
  10. {
  11. String regex = "\\b(?:leng|wid|dep)th\\h+\\d+(?:\\.\\d+)?(?:\\h+cm)?\\b";
  12. String s = "length 10 cm width 2 cm depth 0.5 cm / length 10 cm width 2 depth 0.5 cm\n";
  13.  
  14. Pattern pattern = Pattern.compile(regex);
  15. Matcher matcher = pattern.matcher(s);
  16.  
  17. while (matcher.find()) {
  18. System.out.println(matcher.group(0));
  19. }
  20. }
  21. }
Success #stdin #stdout 0.13s 55832KB
stdin
Standard input is empty
stdout
length 10 cm
width 2 cm
depth 0.5 cm
length 10 cm
width 2
depth 0.5 cm