Python regex get with zero or more occurences within the pattern

python, regex

Solution

The `[]` in your regexp will not group the regexp. It is used to specify the character-class.

Maybe this will work-

\w*/{0,10}

And for grouping just use `()`

(\w*/){0,10}

EDIT

Based on your edited question, I think what you want is that 0-3 occurrences of the directory name match and then a file name matches too.

Assuming only alphabets in file names (and an optional upto three character extension)

^((?:[:\w]+/){0,3})(\w+(?:\.\w{1,3})?)?$

This is very big but can be broken into two parts

This is what you already have

((?:[:\w]+/){0,3})

And what I add

(\w+(?:\.\w{1,3})?)?

This is an optional filename at the end. (If it is not optional you can remove the last `?`). The filename itself can either be made up of only alphabtes or also have an extension of maximum 3 characters

Adding `^` and `$` will stop spurous matches

>>> pat=re.compile('^((?:[:\w]+/){0,3})(\w+(?:\.\w{1,3})?)?$')
>>> my_str='fwefw/wfwf/wefwf/dde.cdf'
>>> pat.search(my_str).groups()
('fwefw/wfwf/wefwf/', 'dde.cdf')
>>> my_str='fwefw/dde.cdf'
>>> pat.search(my_str).groups()
('fwefw/', 'dde.cdf')

Problem

I've a working regex that's giving me the results that I want, yet it doesn't contain the safety it needs. (Fool-proof) Lets say I have a regex that matches parts of a path, something along the lines of: ``` import re path = "C:/Projects/foo/dev/model/props/furniture/couch/data/fbx" regex = re.compile("(.+)/dev/model/(.+)/(.+)/data/fbx") m = regex.search(path) if m: print m.groups() # ('C:/Projects/foo', 'props/furniture', 'couch') ``` I want to be able to replace the match any character up till the following part of the regex with something that will match one or more folders. Let's say we define a folder for simplicity as word characters (none or more) ending with a slash it would be: ``` [\w]*/ ``` And I want to group zero to ten of those, how would I do that? In my mind I had something like (note that this doesn't work!): ``` # match any number of word characters ending with a slash zero to ten times ([[\w]*/]{0,10}) # match any number of word characters ending with a slash zero to one time ([[\w]*/]?) ``` EDIT: Based on RedBaron and jamylak's answer I came up with the following: ``` ((?:[:\w]+/){0,3}) ``` This will group zero to three occurences [:\w] characters ending with a slash '/'. With the ?: at the beginning of the group it's not being send back to the matched groupings. So yet the outer group that combines them is. Therefore we only get the fully grouped result back. The only issue with this is that I want the last part to also possibly match a file. (So not ending with a slash.) I even prefer to get it back without the trailing slash from the regex, but I could also easily strip that of the end of the result. Any feedback is greatly appreciated. If this is the way to go I'll add it as an answer. EDIT: It's related to: Finding folders back based on a predefined folder structure UPDATE/EDIT: Based on all the answers given so far I've come up with a variety of tries, yet they all end up extremely slow in the end. ``` import re path = "C:/Projects/foo/dev/model/props/furniture/couch/data/fbx" regex = re.compile(r"""((?:^(?:[\w:]+/?)+)|(?:(?<=/)(?:[\w]+/?)+))/dev/model/""") print 'search start' m = regex.search(path) print 'search done' if m: print 'match', m, m.groups() else: print 'no match' ``` I'm not entirely sure how to speed this up!

Original source