Reg Ex help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • don

    #1

    Reg Ex help

    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.

    Thanks in advance,
    Don

  • James Thiele

    #2
    Re: Reg Ex help

    don wrote:[color=blue]
    > 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.
    >
    > Thanks in advance,
    > Don[/color]

    Not regex, but does this do what you want?[color=blue][color=green][color=darkred]
    >>> s = "/main/parallel_branch _1/release_branch_ 1.0/dbg_for_python/CHECKEDOUT"
    >>> s = s + " from /main/parallel_branch _1/release_branch_ 1.0/4"
    >>> s.split('/')[4][/color][/color][/color]
    'dbg_for_python '

    Comment

    • Tim Chase

      #3
      Re: Reg Ex help

      > /main/parallel_branch _1/release_branch_ 1.0/dbg_for_python/CHECKEDOUT[color=blue]
      > 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.[/color]

      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



      Comment

      • Aaron Barclay

        #4
        Re: Reg Ex help

        Hi don,

        there may well be a better way then regex, although I find them usefull
        and use them a lot.

        The way they work would be dependant on knowing some things. For
        example, if the dir you are after is always 4
        deep in the structure you could try something like...

        path =
        '/main/parallel_branch _1/release_branch_ 1.0/dbg_for_python/CHECKEDOUT

        from /main/parallel_branch _1/release_branch_ 1.0/4'

        p = re.compile('/\S*/\S*/\S*/(\S*)/')
        m = re.search(p, path)
        ..
        if m:
        print m.group(1)


        This is a good reference...


        Hope that helps,
        aaron.

        don wrote:
        [color=blue]
        >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.
        >
        >Thanks in advance,
        >Don
        >
        >
        >[/color]

        Comment

        • Mirco Wahab

          #5
          Re: Reg Ex help

          Hi don[color=blue]
          > 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.[/color]

          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.

          Comment

          • Bruno Desthuilliers

            #6
            Re: Reg Ex help

            don a écrit :[color=blue]
            > 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.[/color]

            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('CH ECKEDOUT') - 1]

            Comment

            • Edward Elliott

              #7
              Re: Reg Ex help

              Bruno Desthuilliers wrote:[color=blue]
              > don a écrit :[color=green]
              >> Also if there is a better way than using regex, please let me know.[/color]
              >
              > 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('CH ECKEDOUT') - 1][/color]

              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.

              Comment

              • Paddy

                #8
                Re: Reg Ex help

                If you have strings of all the CHECKEDOUT (is this from the lsco
                command?), then the following might work for you:
                [color=blue][color=green][color=darkred]
                >>> s ="/main/parallel_branch _1/release_branch_ 1.0/dbg_for_python/CHECKEDOUT from /main/parallel_branch _1/release_branch_ 1.0/4"
                >>> s.split()[0].split('/')[-2][/color][/color][/color]
                'dbg_for_python '[color=blue][color=green][color=darkred]
                >>>[/color][/color][/color]

                - Paddy.

                Comment

                • Paddy

                  #9
                  Re: Reg Ex help

                  P.S.

                  This is how it works:
                  [color=blue][color=green][color=darkred]
                  >>> s ="/main/parallel_branch _1/release_branch_ 1.0/dbg_for_python/CHECKEDOUT from /main/parallel_branch _1/release_branch_ 1.0/4"
                  >>> s[/color][/color][/color]
                  '/main/parallel_branch _1/release_branch_ 1.0/dbg_for_python/CHECKEDOUT
                  from /main/parallel_branch _1/release_branch_ 1.0/4'[color=blue][color=green][color=darkred]
                  >>> s.split()[/color][/color][/color]
                  ['/main/parallel_branch _1/release_branch_ 1.0/dbg_for_python/CHECKEDOUT',
                  'from', '/main/parallel_branch _1/release_branch_ 1.0/4'][color=blue][color=green][color=darkred]
                  >>> s.split()[0][/color][/color][/color]
                  '/main/parallel_branch _1/release_branch_ 1.0/dbg_for_python/CHECKEDOUT'[color=blue][color=green][color=darkred]
                  >>> s.split()[0].split('/')[/color][/color][/color]
                  ['', 'main', 'parallel_branc h_1', 'release_branch _1.0',
                  'dbg_for_python ', 'CHECKEDOUT'][color=blue][color=green][color=darkred]
                  >>> s.split()[0].split('/')[-1][/color][/color][/color]
                  'CHECKEDOUT'[color=blue][color=green][color=darkred]
                  >>> s.split()[0].split('/')[-2][/color][/color][/color]
                  'dbg_for_python '[color=blue][color=green][color=darkred]
                  >>> s ="/main/parallel_branch _1/release_branch_ 1.0/dbg_for_python/CHECKEDOUT from /main/parallel_branch _1/release_branch_ 1.0/4"
                  >>> s.split()[0].split('/')[-2][/color][/color][/color]
                  'dbg_for_python '[color=blue][color=green][color=darkred]
                  >>>[/color][/color][/color]

                  - Paddy.

                  Comment

                  • bruno at modulix

                    #10
                    Re: Reg Ex help

                    Edward Elliott wrote:
                    (snip)[color=blue][color=green]
                    >>don a écrit :
                    >>[color=darkred]
                    >>>Also if there is a better way than using regex, please let me know.[/color]
                    >>[/color][/color]
                    (snip)[color=blue]
                    >
                    > 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][/color]

                    Much cleaner than mine. I shouldn't try to code when it's time to bed !-)


                    --
                    bruno desthuilliers
                    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
                    p in 'onurb@xiludom. gro'.split('@')])"

                    Comment

                    • Edward Elliott

                      #11
                      Re: Reg Ex help

                      bruno at modulix wrote:
                      [color=blue]
                      > parts = s.replace(' ', '/').strip('/').split('/')
                      > branch = parts[parts.index('CH ECKEDOUT') - 1]
                      >
                      > Edward Elliott wrote:[color=green]
                      >>
                      >> marker = s.index('/CHECKEDOUT')
                      >> branch = s [s.rindex('/', 0, marker) + 1 : marker][/color]
                      >
                      > Much cleaner than mine. I shouldn't try to code when it's time to bed !-)[/color]

                      Not terribly readable though, hard to tell what the magic slice indexes
                      mean. Yours is easier to follow. I think I'd just use a regex though.

                      --
                      Edward Elliott
                      UC Berkeley School of Law (Boalt Hall)
                      complangpython at eddeye dot net

                      Comment

                      • Anthra Norell

                        #12
                        Re: Reg Ex help

                        >>> se = SE.SE ('<EAT> "~/[A-Za-z0-9_]+/CHECKEDOUT~==" | /= CHECKEDOUT=')[color=blue][color=green][color=darkred]
                        >>> se[/color][/color][/color]
                        ('/main/parallel_branch _1/release_branch_ 1.0/dbg_for_python/CHECKEDOUT')
                        'dbg_for_python '

                        If I understand your problem, this might be a solution. It is a stream
                        editor I devised on the impression that it could handle in a simple manner a
                        number of relatively simple problems on this list for which no
                        commensurately simple methodologies seem to exist. I intend to propose it to
                        the group when I finish the doc. Meantime who do I propose it to?

                        Frederic


                        ----- Original Message -----
                        From: "don" <abhidon@gmail. com>
                        Newsgroups: comp.lang.pytho n
                        To: <python-list@python.org >
                        Sent: Thursday, May 11, 2006 7:39 PM
                        Subject: Reg Ex help

                        [color=blue]
                        > 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.
                        >
                        > Thanks in advance,
                        > Don
                        >
                        > --
                        > http://mail.python.org/mailman/listinfo/python-list[/color]

                        Comment

                        Working...