0

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!

1 Answer 1

5

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 _.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.