0

I have a long link that looks like this:

link = 'http://....rech_cod_cat=1&rech_cod_rub=&rech_cod_typ=&rech_cod_sou_typ=&rech_cod_pay=TN&rech_cod_reg=&rech_cod_vil=&rech_cod_loc=&rech_prix_min=&rech_prix_max=&rech_surf_min=&rech_surf_max=&rech_age=&rech_photo=&rech_typ_cli=&rech_order_by=31&rech_page_num=829'

I am trying to match the integer at the end to find the number of the last page, my first idea was this :

m = re.search(r'num=(\d)+$', link)

but this only matches the last digit, I pulled the + inside the match group just to try it

m = re.search(r'num=(\d +)$', link)

and now it works but I have no idea why. Shouldn't both pattern match the same group ? Can someone explain how these two differ ? I am also confused as why it matched the last digit and not the first if it was going to match only one digit.

Domarius
  • 19
  • 7
  • 2
    "*and now it works but I have no idea why. Shouldn't both pattern match the same group ? *" the brackets denote a group. There is definitely a difference between "capture exactly one digit in a group" and "capture one or more digits in the group". – VLAZ Aug 22 '23 at 04:44
  • 1
    Why would you reinvent the wheel? https://docs.python.org/3/library/urllib.parse.html#urllib.parse.parse_qs – Bharel Aug 22 '23 at 04:52
  • 4
    Both patterns DID match all of the digits. The first one matched the digits one at a time, each one overwriting the previous capture of a single digit. The second one captured all of them at once. – jasonharper Aug 22 '23 at 04:59
  • @jasonharper that's exactly what I wanted to know. Thank you very much – Domarius Aug 22 '23 at 14:06

1 Answers1

1

Based on the regex link below, the regex actually matches all of the digits. However, since you placed it in a parenthesis (\d), this means you want to capture the group with only one digit. It also gets the last digit as the group since you have a $ that specified that this is the end of the line

Please refer to the following link for the explanation:

https://regex101.com/r/0xDnOG/1

For the second regex, this also matches all of the digits. However, since you placed the + inside the parenthesis (\d+), this means you want to capture the group with multiple digits (one and multiple quantifier).

Please refer to the following link for the explanation:

https://regex101.com/r/PU52hx/1

lorvindc
  • 69
  • 1
  • 7