views:

59

answers:

1

I have a string that looks like:

http://www.example.com/index.do/blah/1_44/asdf/asdf/asdf
http://www.example.com/index.do/blah/1_44_2342/asdf/asdf/asdf

I need to grab the value 44 from the above, ofcourse '44' could be any number. The number '44' always is prefixed with a _, and after it could be another _ or /.

I have no idea of the java regex API, so as guidance would be appreciated!

+5  A: 

It's primarily the Pattern and Matcher classes you're interested in.

String url = "http://www.example.com/index.do/blah/1_44/asdf/asdf/asdf";
Pattern p = Pattern.compile("_(\\d+)");
Matcher m = p.matcher(url);
if (m.find()) {
  int number = Integer.valueOf(m.group(1));
}

This pattern finds the first sequence of one or more digits after the first _.

cletus