tags:

views:

119

answers:

2

I need to grab two chunks of a string to obtain the values. I've wrapped them in curly braces for demo purposes. I assume I need to use a bit of regex for this? I'm not asking for someone to spoonfeed me the code, but I'm struggling to figure out how to match more than one part of a string. Once I understand how to do this the world is my oyster.

/path/to/image/{name}-sm{1}.jpg

As you can see, I need to grab {name} and {n} ( 1 in this case ).

+1  A: 

Like this? :)

/path/to/image/(name:\w+)-sm(id:\d+)\.jpg
Veton
A: 

Here's a small javascript snippet you can use as a guide:

>>> re = /(\w+)-sm(\d+)\.jpg/
>>> text = "/path/to/image/name-sm1.jpg"
>>> re.exec(text)

The output you get is:

["name-sm1.jpg", "name", "1"]

Another example:

>>> text = "/path/to/image/namefff-sm12.jpg"
>>> re.exec(text)
["namefff-sm12.jpg", "namefff", "12"]

Hope you find this useful...

Santi