code: public static int getIntSelector(SlingHttpServletRequest request, Pattern groupPattern, int defaultValue) {
          String[] selectors = request.getRequestPathInfo().getSelectors();
          for (String selector : selectors) {
              Matcher matcher = groupPattern.matcher(selector);
              if (matcher.matches()) {
                  try {
                      return Integer.parseInt(matcher.group(1));
                  } catch (NumberFormatException nfex) {
                      // ok, try next
                  }
              }
          }
          return defaultValue;
      }
docs: Retrieves a number in the selectors and returns it if present otherwise the default value.
  
  @param request      the request object with the selector info
  @param groupPattern the regex to extract the value - as group '1'; e.g. 'key([\d]+)'
  @param defaultValue the default number value