0

I have 2 types of URLS

the first one has numbers at the end of url

www.example.fr/drive/cat.productlist.pagination_0.topage/2?t:ac=3686962/3686315

the second one:

www.example.com/some/cat.productlist.pagination_0.topage/4?t:ac=someText

how can i know my input is like first one or second one?

i tried this:

myURL = http://www.example.com/some/cat.productlist.pagination_0.topage/4?t:ac=someText
                        parsed_url = urlparse.urlparse(myURL)
                        number2, number3 = urlparse.parse_qs(parsed_url.query)["t:ac"][0].split("/")

                        if ( isinstance( number2, numbers.Number) and isinstance( number3, numbers.Number) ) :
                            print "first"
                        else :
                            print "second"
7
  • 2
    Why have you mentioned regex here? Commented Apr 28, 2016 at 9:18
  • @DanielRoseman I tried by urlParse, but i want to do that by regex Commented Apr 28, 2016 at 9:19
  • @parik, did you know regex stands for regular expression? Whats the meaning of your title? Commented Apr 28, 2016 at 9:26
  • @MohitC I would like to know how can i solve my problem with regex, excuse-me if my title is not clear, i read this stackoverflow.com/help/how-to-ask and i don't undrestand why my title is not good, i edit my title Commented Apr 28, 2016 at 9:29
  • Why did you @ me? I didn't criticise your title. Commented Apr 28, 2016 at 9:34

3 Answers 3

2

I don't know why you want to do this with regex, but this would work:

if re.search(r't:ac=(\d+)', myURL):
    print "numbers"
Sign up to request clarification or add additional context in comments.

Comments

1

You can use a regex to check if the url ends with numbers or letters i.e.:

if re.search(r"\d+$", url):
    # url ends with numbers
if re.search("[a-z]+$", url, re.IGNORECASE):
    # url ends with letters

Comments

1

your code is more or less correct already, but usually in python you just convert the data into the data format you wish, until it breaks (ask forgiveness not permission principle)

So you can try something like this (in Python3),

from urllib.parse import urlparse, parse_qs

myURL = 'http://www.example.com/some/cat.productlist.pagination_0.topage/4?t:ac=someText'

query = parse_qs(urlparse(myURL).query)

try:
    number2, number3 = query.get('t:ac', [''])[0].split('/')

    # do something with the numbers
    new_number = int(number2) + int(number3)

    print('first')
except ValueError:
    # t:ac does not have a slash in it
    print('second')

(Python2)

from __future__ import print_function
from urlparse import urlparse, parse_qs

myURL = 'http://www.example.com/some/cat.productlist.pagination_0.topage/4?t:ac=someText'

query = parse_qs(urlparse(myURL).query)

try:
    number2, number3 = query.get('t:ac', [''])[0].split('/')

    # do something with the numbers
    new_number = int(number2) + int(number3)

    print('first')
except ValueError:
    # t:ac does not have a slash in it
    print('second')

it is not that asking permission is not possible, just that it probably wouldn't look as elegant

if number2.isdigit() and number3.isdigit():
    print("first")
else :
    print("second")

2 Comments

also if you are not writing code for existing python2 application, or requiring the use of old libraries, please write only in python 3 (:
Thanks for your answer, i work with Scrapy and i have to write in python 2.7

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.