Reg Ex help

Edward Elliott nobody at 127.0.0.1
Thu May 11 18:41:54 EDT 2006


Bruno Desthuilliers wrote:
> don a écrit :
>> Also if there is a better way than using regex, please let me know.
> 
> s ="/main/parallel_branch_1/release_branch_1.0/dbg_for_python/CHECKEDOUT
>   from /main/parallel_branch_1/release_branch_1.0/4"
> parts = s.replace(' ', '/').strip('/').split('/')
> branch = parts[parts.index('CHECKEDOUT') - 1]

I wouldn't call these better (or worse) than regexes, but a slight variation
on the above:

marker = s.index('/CHECKEDOUT')
branch = s [s.rindex('/', 0, marker) + 1 : marker]

This version will throw exceptions when the marker isn't found, which may or
may not be preferable under the circumstances.




More information about the Python-list mailing list