// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.util.regex.*;

public class NonCapturingRegex {
   private static Pattern p =
      Pattern.compile(
         "(?:Afstand\\s*)((\\d+.\\d+)|(\\d+))(?:\\s*km\\s+)");
   public static void findkm(String s) {
      Matcher m = p.matcher(s);
      while (m.find()) {
         System.out.println(m.group(1));
      }
   }
   public static void main(String[] args) throws Exception {
      String s = "Afstand 5 km\r\n" +
                 "Afstand 5.5 km\r\n" +
                 "Afstand  6  km\r\n" +
                 "Afstand7km\r\n";
      findkm(s);
   }
}
