String accessRegex = ".*userNameId\\-(\\d+).*"; String text = "2012-10-26 14:41:30,748 userNameId-777 from IP-10.232.25.144 invoked URL-http://xxx/hello.jsonp"; Pattern pattern = Pattern.compile(accessRegex); Matcher matcher = pattern.matcher(text); if (matcher.find()) { System.out.println(matcher.group(1)); }