Hello!
You can also get a dictionary out of the matched groups...

import re

name = 'hello_v001.ma'

regex = '^(?P<name>.*)_[vV](?P<version>[0-9]{3})\.(?P<extension>.*)$'

# notice the ?P<version> inside of the () groups

match = re.match(regex, name)

match.groupdict()

# Result: {'version': '001', 'name': 'hello', 'extension': 'ma'} #


Although the expression gets a little bit more obscure

Cheers!

Eduardo


On Sun, May 4, 2014 at 1:41 PM, Marcus Ottosson <[email protected]>wrote:

> Very useful! Always wondered why that command was called "group" when I
> only ever got a single result back. :)
>
>
> On 4 May 2014 17:16, Eduardo Grana <[email protected]> wrote:
>
>> Hello Jeremy,
>> One thing i found very usefull is the groups method of re.match,
>> so you can get parts of the matched pattern
>> for example,
>>
>> >>> import re
>> >>> st = 'hello_v001'
>> >>> regex = '(.*)_[vV]([0-9]{3})$' # notice the ( ) to separate the parts
>> you get later, the $ represents the end of the string, the .* will match
>> anything
>> >>> re.match(regex, st)
>> <_sre.SRE_Match object at 0x01DC53C8>
>> >>> r = re.match(regex, st)
>> >>> r.groups()
>> ('hello', '001')
>>
>> Cheers!
>> Eduardo
>>
>>
>>
>>
>> On Sun, May 4, 2014 at 8:18 AM, Justin Israel <[email protected]>wrote:
>>
>>> The re module also has a case insensitive flag:
>>>
>>> re.search(r'_v\d+', 'hello_v001', re.I)
>>>
>>> You can also limit it to a 3 padded version scheme if you want to go
>>> that far:
>>>
>>> re.search(r'_v\d{3}', 'hello_v001', re.I)
>>> On May 4, 2014 11:07 PM, "Anthony Tan" <[email protected]>
>>> wrote:
>>>
>>>> If the leading 'v' is the relevant thing, would '_[Vv][0-9]+' do?
>>>>
>>>> (Its just hunting for an underscore, lowercase OR uppercase v, then a
>>>> string of numbers)
>>>>  On 4 May 2014 20:47, Jeremy YeoKhoo <[email protected]> wrote:
>>>>
>>>> Hey guys,
>>>>
>>>> This should be fairly easy for you guys who know regex. If I have a
>>>> string that I want to enable versioning, so say for an example I want to
>>>> recognize a string if it contains ['v001', 'v002', etc...]
>>>> I have something like this...
>>>>
>>>> remp= re.search('[0-9]+', 'hello_v001')
>>>> print temp.group()
>>>>
>>>> but I dont want it to return when:
>>>> remp= re.search('[0-9]+', 'hello_001')
>>>> print temp.group()
>>>>
>>>> Thanks
>>>> -Jeremy
>>>>
>>>>  --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Python Programming for Autodesk Maya" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to [email protected].
>>>> To view this discussion on the web visit
>>>> https://groups.google.com/d/msgid/python_inside_maya/2269441f-f499-4a9a-9e08-8226214db73a%40googlegroups.com<https://groups.google.com/d/msgid/python_inside_maya/2269441f-f499-4a9a-9e08-8226214db73a%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>> .
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>>  --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Python Programming for Autodesk Maya" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to [email protected].
>>>> To view this discussion on the web visit
>>>> https://groups.google.com/d/msgid/python_inside_maya/201405041107.s44B7L7K011303%40atl4mhob05.myregisteredsite.com<https://groups.google.com/d/msgid/python_inside_maya/201405041107.s44B7L7K011303%40atl4mhob05.myregisteredsite.com?utm_medium=email&utm_source=footer>
>>>> .
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>  --
>>> You received this message because you are subscribed to the Google
>>> Groups "Python Programming for Autodesk Maya" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>>  To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA3Z7NVysf56bmqWX8dzjJ9i6bNhnJdy%3DoxBG4qK8D4jug%40mail.gmail.com<https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA3Z7NVysf56bmqWX8dzjJ9i6bNhnJdy%3DoxBG4qK8D4jug%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>
>>
>> --
>> Eduardo Graña
>> www.eduardograna.com.ar
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Python Programming for Autodesk Maya" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>>  To view this discussion on the web visit
>> https://groups.google.com/d/msgid/python_inside_maya/CACt6GrmdFrv2E0U1qxoCFMmPi-3xwavk4JNMiWjXfsJ5mY-MwQ%40mail.gmail.com<https://groups.google.com/d/msgid/python_inside_maya/CACt6GrmdFrv2E0U1qxoCFMmPi-3xwavk4JNMiWjXfsJ5mY-MwQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> --
> *Marcus Ottosson*
> [email protected]
>
> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOAkKjQE5%3DO3o%2B3FMa3bVotg33FJmnUqfjrrfbX6gW2yOQ%40mail.gmail.com<https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOAkKjQE5%3DO3o%2B3FMa3bVotg33FJmnUqfjrrfbX6gW2yOQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Eduardo Graña
www.eduardograna.com.ar

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CACt6GrnS1BxrvCHq4RdGL7idSoyizNBSSRLQOjD8CWiUcZCWww%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to