Reg Ex help

Tim Chase python.list at tim.thechases.com
Thu May 11 14:07:12 EDT 2006


> /main/parallel_branch_1/release_branch_1.0/dbg_for_python/CHECKEDOUT
> from /main/parallel_branch_1/release_branch_1.0/4
> 
> I want to write a regex that gives me the branch the file was
> checkedout on ,in this case - 'dbg_for_python'
> 
> Also if there is a better way than using regex, please let me know.

Well, if you have it all in a single string:

s = 
"/main/parallel_branch_1/release_branch_1.0/dbg_for_python/CHECKEDOUT 
from /main/parallel_branch_1/release_branch_1.0/4"

you can do

branch = s.split("/")[4]

which returns the branch, assuming the path from root is the 
same for each item in question.

If not, you can tinker with something like

r = re.compile(r'/([^/]*)/CHECKEDOUT')
m = r.match(s)

and which should make m.groups(1) the resulting item.  You 
don't give much detail regarding what is constant (the 
number of subdirectories in the path?  the CHECKEDOUT 
portion?, etc) so it's kinda hard to figure out what is most 
globally applicable.

-tkc






More information about the Python-list mailing list