-2

Hi I have an URL like "/yyyyyy/xm", where x can be an integer denoting the number of minutes. I need to parse and get this value. Any idea of how this can be using regex or String.split() method? The pattern of the URL is always the same like for example:

/magnetic/20m should give me the value 20
/temperature/500m should give me the value 500 

Thanks in advance!

user1741274
  • 729
  • 3
  • 11
  • 25
  • The following Url can help you in finding your answer. http://stackoverflow.com/questions/1903252/extract-integer-part-in-string – Rohit Feb 21 '13 at 07:50
  • 2
    What have you tried? What does your attempt look like? Where are you running into trouble? – T.J. Crowder Feb 21 '13 at 07:50

2 Answers2

3

The following should work:

/.*?/(\d+)

You just need to access to the 1st group of the match, and you'll get the numbers there.

Edit:
In the future, finding the regex by yourself. That's a pretty straightforward regex question.

Oscar Mederos
  • 26,873
  • 20
  • 76
  • 120
  • 1
    there should be `m` after `(\d+)` else it would also match `www.site.com/123` – Anirudha Feb 21 '13 at 07:57
  • @Some1.Kill.The.DJ two things: 1) are you sure? I only see one `/` on `www.site.com/123` and 2) suppose it matches `www.site.com/123`, then it would match `www.site.com/123m` too. I think you should design regular expressions in an specific context. Do you know if he is trying to parse a file where each line only has `/some-text/XXXXm`? – Oscar Mederos Feb 21 '13 at 08:00
1

And if you don't like regexp...

        String txt = "/magnetic/20m";
        String[] components = txt.split("/");
        String lastComponent = components[components.length - 1];
        int result = Integer.parseInt(lastComponent.replace("m", ""));
Piotr
  • 1,668
  • 1
  • 22
  • 38