new to java regex, how to grab this part of the string
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 g开发者_如何学Gouidance would be appreciated!
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 _
.
精彩评论