-1

I am new to Python and would like to know how to have build a regex pattern to match a URL

I have the following code in Java and it works. I need to have a similar one in python

Java:

  URI uri = new URI("http://localhost:8080")

  Matcher m = Pattern.compile("(.*)" + "/client" + "/([0-9]+)")
  .matcher(uri.getPath());

Could someone guide me with having an equivalent regex in Python

user3451476
  • 297
  • 1
  • 4
  • 17
  • @TigerhawkT3 There are basically two questions here: "How do I get the path from a URL in Python?" and "How do I use regular expressions in Python?" The first of those two questions is answered well by the question you made this a duplicate of. The second question may very well also be a duplicate, but the question you linked to is certainly not relevant. – user94559 Jul 26 '16 at 03:35
  • @smarx - The [second answer to that question](http://stackoverflow.com/a/835527/2617068) (voted more highly than the accepted answer, too) has a complete regular expression for URLs. I don't see anything at all, remotely, whatsoever in this question that is asking the general case of how to use regular expressions in Python, which is good, because that would be far too broad to even consider answering. The duplicate is ideal. – TigerhawkT3 Jul 26 '16 at 03:38
  • @TigerhawkT3 The question here is how to use a regular expression to get "/foo" and "12345" out of a string like "/foo/client/12345". The second question in the duplicate is also completely unrelated. – user94559 Jul 26 '16 at 03:39

2 Answers2

3

Why not use urlparse? Batteries included :-).

>>> import urlparse
>>> urlparse.urlparse("http://localhost:8080")
ParseResult(scheme='http', netloc='localhost:8080', path='', params='', query='', fragment='')
mgilson
  • 300,191
  • 65
  • 633
  • 696
1

Here's the equivalent in Python 2.7:

import re
from urlparse import urlparse

url = urlparse('http://localhost:8080')

match = re.match(r'(.*)/client/([0-9]+)', url.path)

EDIT

Here's how you would use match to get the individual components (just guessing as to what you want to do next):

if match:
    prefix = match.group(1)
    client_id = int(match.group(2))
user94559
  • 59,196
  • 6
  • 103
  • 103