0

What would be the equivalent of this (Javascript) in python?

var patt = /sub(\d+)\.domain\.com\/(\d+)/
  , m    = url.match(patt)
  , url = 'http://sub' + m[1] + '.domain.com/' + m[2]

I'm new at Python and not quite understanding the regex system yet :(

1
  • @MikePennington ah, yes...sorry forgot to add that! Commented May 7, 2012 at 22:16

2 Answers 2

1

You've pretty much already got it

>>> x = re.search("sub(\d+)\.domain\.com\/(\d+)","sub123.domain.com/546").groups()
('123', '546')
>>> url = "%s blah blah %s" % x
Sign up to request clarification or add additional context in comments.

Comments

1

The rough equivalent of your code in Python would be

import re

url = 'http://sub36.domain.com/54'

patt = re.compile("sub(\d+)\.domain\.com\/(\d+)")
m = patt.search(url)
url = 'http://sub'+m.group(1)+'.domain.com/'+m.group(2)

Comments

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.