Reg Ex help

Mirco Wahab wahab at chemie.uni-halle.de
Thu May 11 16:12:48 EDT 2006


Hi don
> I have a string  from a clearcase cleartool ls command.
> /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.

This is a good situation where Regex come into play,
because all other solutions won't catch on different
string structures easily.

If you know that you will need the string
before CHECKEDOUT, you can, for example use some
nice positive lookahead (mentioned today here)

pseudo: take all strings between / ... / and
return 'em if the next thing is CHECKEDOUT
(or something else):

        / ([^/]+) / (?=CHECKEDOUT)

The ([^/]+) means  ^/ (not /) in a character
class, [^/]+ one or more than one times
and ([^/]+) capture it by (..)

The code:

   import re

   t = '/main/parallel_branch_1/release_branch_1.0/dbg_for_python/CHECKEDOUT from /main/p...'
   r = r'/([^/]+)/(?=CHECKEDOUT)'

   # print re.search(r, t).group(1)

would do the job, independent of the structure
of string - except the /CHECKEDOUT thing (which
has to be there)

If there are 'better ways' - that depends on
'better ways for whom?'. If you can handle
the Railgun, why bother with the Pistols ;-)

Regards

M.



More information about the Python-list mailing list