Cleaning strings with Regular Expressions

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

    #1

    Cleaning strings with Regular Expressions

    Hello,

    I often find myself cleaning up strings like the following:

    setAttr ".ftn" -type "string" /assets/chars/
    /boya/geo/textures/lod1/ppbhat.tga";

    Using regular expressions, the best I can do so far is using the re.sub
    command but it still takes two lines. Can I do this in one line? Or
    should I be approaching this differently? All I want to end up with is
    the file name "ppbhat.tga ".

    Python Code:
    lines[indexC]=re.sub("[\s,\S,]*/", "", lines[indexC])
    lines[indexC]=re.sub(".tga[\s,\S,]*", ".tga", lines[indexC])

    Thanks for your time,

    /\/\ason S

  • Fredrik Lundh

    #2
    Re: Cleaning strings with Regular Expressions

    "sheffdog" <masonsheffield @gmail.com> wrote:
    [color=blue]
    > setAttr ".ftn" -type "string" /assets/chars/
    > /boya/geo/textures/lod1/ppbhat.tga";[/color]
    [color=blue]
    > Can I do this in one line?[/color]
    [color=blue][color=green][color=darkred]
    >>> os.path.basenam e("/assets/chars/.../lod1/ppbhat.tga")[/color][/color][/color]
    'ppbhat.tga'

    </F>



    Comment

    • Larry Bates

      #3
      Re: Cleaning strings with Regular Expressions

      May not be what you are looking for, but this works:

      import os
      s='setAttr ".ftn" -type "string" ' \
      '/assets/chars/boya/geo/textures/lod1/ppbhat.tga";'
      fname=os.path.b asename(s.split ()[-1])


      BTW-It does depend on the file/path being the last item
      on the line.

      Larry Bates


      sheffdog wrote:[color=blue]
      > Hello,
      >
      > I often find myself cleaning up strings like the following:
      >
      > setAttr ".ftn" -type "string" /assets/chars/
      > /boya/geo/textures/lod1/ppbhat.tga";
      >
      > Using regular expressions, the best I can do so far is using the re.sub
      > command but it still takes two lines. Can I do this in one line? Or
      > should I be approaching this differently? All I want to end up with is
      > the file name "ppbhat.tga ".
      >
      > Python Code:
      > lines[indexC]=re.sub("[\s,\S,]*/", "", lines[indexC])
      > lines[indexC]=re.sub(".tga[\s,\S,]*", ".tga", lines[indexC])
      >
      > Thanks for your time,
      >
      > /\/\ason S
      >[/color]

      Comment

      • sheffdog

        #4
        Re: Cleaning strings with Regular Expressions

        Using basename works, but leaves the extra stuff at the end.
        Which would have to be removed with another line of code

        I get this--> ppbhat.tga";

        Thanks, for the idea though.
        /\/\ason

        Comment

        • Fredrik Lundh

          #5
          Re: Cleaning strings with Regular Expressions

          "sheffdog" <masonsheffield @gmail.com> wrote:[color=blue]
          > Using basename works, but leaves the extra stuff at the end.
          > Which would have to be removed with another line of code
          >
          > I get this--> ppbhat.tga";[/color]

          if you're trying to parse Maya files, maybe you should start
          by writing a simple Maya parser, and use that to extract the
          relevant strings, *before* passing them to os.path.baselin e?

          </F>



          Comment

          • sheffdog

            #6
            Re: Cleaning strings with Regular Expressions

            Good Idea I'll try that!

            Thanks for your assistance.
            /\/\

            Comment

            • Gary Wilson Jr

              #7
              Re: Cleaning strings with Regular Expressions

              sheffdog wrote:[color=blue]
              > Using regular expressions, the best I can do so far is using the re.sub
              > command but it still takes two lines. Can I do this in one line? Or
              > should I be approaching this differently? All I want to end up with is
              > the file name "ppbhat.tga ".[/color]

              A regular expression to do what you want:[color=blue][color=green][color=darkred]
              >>> s = 'setAttr ".ftn" -type "string" /assets/chars/boya/geo/textures/lod1/ppbhat.tga";'
              >>> s = re.sub(r".*/(.*\.tga).*", r"\1", s)
              >>> s[/color][/color][/color]
              'ppbhat.tga'

              Is a regular expression the best solution?
              That depends on what else you need to do with your data file.

              Comment

              Working...