Regex help please?

52 views
Skip to first unread message

Panupat Chongstitwattana

unread,
Oct 1, 2013, 6:06:55 AM10/1/13
to python_in...@googlegroups.com
Hi.

I have this string

path = r'P:\<various directories>\scene\ch01\seq001\s0090\anim\.......'

I want to split out the ch01, seq001 and s0090 into 3 variables (or a list). Is there a way to do this with regex in a single line? Maybe with re.split(...)

Currently I do it in 3 different steps

find = re.findall(r'ch\d\d', path)
if find: ch = find[0]

find = re.findall(r'seq\d\d', path)
if find: seq = find[0]

etc.

Thanks

Justin Israel

unread,
Oct 1, 2013, 6:43:59 AM10/1/13
to Panupat Chongstitwattana, python_in...@googlegroups.com
You can use a single regular expression, with capture groups:

In [1]: import re

In [2]: path = r'P:\foo\bar\biz\scene\ch01\seq001\s0090\anim\foo\bar\biz'

In [3]: match = re.search(r'(ch\d{2})\\(seq\d{3})\\(s\d{4})', path)

In [4]: if match:
...: print match.groups()
...:
('ch01', 'seq001', 's0090')

In [5]: match.group(1)
Out[5]: 'ch01'

In [6]: match.group(2)
Out[6]: 'seq001'

In [7]: match.group(3)
Out[7]: 's0090'

In [8]: match = re.search(r'ch(\d{2})\\seq(\d{3})\\s(\d{4})', path)

In [9]: print match.groups()
('01', '001', '0090')
> --
> 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 python_inside_m...@googlegroups.com.
> To post to this group, send email to python_in...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Panupat Chongstitwattana

unread,
Oct 9, 2013, 12:01:50 PM10/9/13
to python_in...@googlegroups.com, Panupat Chongstitwattana
Sorry for the late thank you Justin!

Justin Israel

unread,
Oct 9, 2013, 3:05:07 PM10/9/13
to python_in...@googlegroups.com, Panupat Chongstitwattana

No worries. Hope it helped.

--
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 python_inside_m...@googlegroups.com.
Message has been deleted

Chad Dombrova

unread,
Nov 8, 2013, 7:12:52 PM11/8/13
to Maya Python Group
not sure if someone answered this already, but here you go:

>>> re.findall(r'(?:ch\d{2})|(?:seq\d{2})|(?:s\d{4})',  r'P:\<various directories>\scene\ch01\seq001\s0090\anim\.......')
['ch01', 'seq00', 's0090']

{2} means repeat the pattern 2 times
?: means do not capture inside these parentheses



On Fri, Nov 8, 2013 at 5:35 AM, <narayan.si...@gmail.com> wrote:
asd
--
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 python_inside_m...@googlegroups.com.

Justin Israel

unread,
Nov 8, 2013, 10:54:20 PM11/8/13
to python_in...@googlegroups.com
I had previously suggested a solution like this:

    match = re.search(r'ch(\d{2})\\seq(\d{3})\\s(\d{4})', path)
    match.groups()
    # ('01', '001', '0090')

Do you think your version, using multiple OR'd patterns might produce a lot of unwanted and non-specifically ordered matches?

    aPath = r'P:\<various directories>\scene\ch01\seq001\s0090\anim\search02_foo\apples1000.......'
    re.findall(r'(?:ch\d{2})|(?:seq\d{2})|(?:s\d{4})',  aPath)
    #  ['ch01', 'seq00', 's0090', 'ch02', 's1000']

I figure matching show/scene/shot (or variation) is a pretty common studio pattern to match and would probably be best suited as one pattern with capture groups. It might even be nice to capture named groups:

    match = re.search(r'ch(?P<ch>\d{2})\\seq(?P<seq>\d{3})\\s(?P<shot>\d{4})', path)
    match.groupdict()
    # {'ch': '01', 'seq': '001', 'shot': '0090'}




Reply all
Reply to author
Forward
0 new messages