1

I'm pretty new to regular expressions. I've worked out the string (?i)\$url\[([0-9])\] to match what I'm looking for. I want a string like "this is $url[2] a string" to be passed through that regex, have 2 passed to a function, which returns a string, and $url[2] is replaced with that string. How do I do that?

1
  • Does the regex work? Have you learned Python? What did you try? Commented Aug 20, 2011 at 22:04

1 Answer 1

3
def myrepl(match):
    num = int(match.group(1))
    # Do something here
    return str(num) # this will replace the $url[2]

mystr = "this is $url[2] a string"

print re.sub(r'\$url\[([0-9])\]', myrepl, mystr, flags=re.IGNORECASE)
Sign up to request clarification or add additional context in comments.

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.