1

I'm creating a URL parser and have three kind of URLs from which I would like to extract the number portion from the end of the URL and increment the extracted number by 10 and update the URL. I'm trying to use regex to extract but I'm new to regex and having trouble.

These are three URL structures of which I'd like to increment the last number portion of:

  1. Increment last number 20 by 10:

    http://forums.scamadviser.com/site-feedback-issues-feature-requests/20/
    
  2. Increment last number 50 by 10:

    https://forums.questionablecontent.net/index.php/board,1.50.html
    
  3. Increment last number 30 by 10:

    https://forums.comodo.com/how-can-i-help-comodo-please-we-need-you-b39.30/
    
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Horse Voice
  • 8,138
  • 15
  • 69
  • 120
  • Why create another wheel? Between Ruby's URI and [Addressable::URI](https://github.com/sporkmonger/addressable) there's a lot of well tested code. – the Tin Man Jan 12 '17 at 18:48
  • We'd like to see your attempt toward solving this, rather than write code for you that has nothing to do with what you've tried. – the Tin Man Jan 12 '17 at 18:51

4 Answers4

2

With \d+(?!.*\d) regex, you will get the last digit chunk in the string. Then, use s.gsub with a block to modify the number and put back to the result.

See this Ruby demo:

strs = ['http://forums.scamadviser.com/site-feedback-issues-feature-requests/20/', 'https://forums.questionablecontent.net/index.php/board,1.50.html', 'https://forums.comodo.com/how-can-i-help-comodo-please-we-need-you-b39.30/']
arr = strs.map {|item| item.gsub(/\d+(?!.*\d)/) {$~[0].to_i+10}}

Note: $~ is a MatchData object, and using the [0] index we can access the whole match value.

Results:

http://forums.scamadviser.com/site-feedback-issues-feature-requests/30/
https://forums.questionablecontent.net/index.php/board,1.60.html
https://forums.comodo.com/how-can-i-help-comodo-please-we-need-you-b39.40/
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
1

Try this regex:

\d+(?=(\/)|(.html))

It will extract the last number.

Demo: https://regex101.com/r/zqUQlF/1


Substitute back with this regex:

(.*?)(\d+)((\/)|(.html))

Demo: https://regex101.com/r/zqUQlF/2

Mohammad Yusuf
  • 16,554
  • 10
  • 50
  • 78
0

this regex matches only the last whole number in each URL by using a lookahead (which 'sees' patterns but doesn't eat any characters):

\d+(?=\D*$)

online demo here.

Scott Weaver
  • 7,192
  • 2
  • 31
  • 43
0

Like this:

urls = ['http://forums.scamadviser.com/site-feedback-issues-feature-requests/20/', 'https://forums.questionablecontent.net/index.php/board,1.50.html', 'https://forums.comodo.com/how-can-i-help-comodo-please-we-need-you-b39.30/']
pattern = /(\d+)(?=[^\d]+$)/

urls.each do |url|
    url.gsub!(pattern) {|m|  m.to_i + 10}
end

puts urls

You can also test it online here: https://ideone.com/smBJCQ

Z-Bone
  • 1,534
  • 1
  • 10
  • 14