-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

3
  • @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. Commented Jul 26, 2016 at 3:35
  • @smarx - The second answer to that question (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. Commented Jul 26, 2016 at 3: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. Commented Jul 26, 2016 at 3:39

2 Answers 2

3

Why not use urlparse? Batteries included :-).

>>> import urlparse
>>> urlparse.urlparse("http://localhost:8080")
ParseResult(scheme='http', netloc='localhost:8080', path='', params='', query='', fragment='')
Sign up to request clarification or add additional context in comments.

Comments

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))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.