Regular expression

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • borthouth
    New Member
    • Nov 2007
    • 7

    Regular expression

    Hi,

    I have multiple files with different filenames and want to test out regular expressions.

    I can use regexp=".*" to pick up all files, and also regexp=".*\.xxx $" to pick up all files ending with ".xxx".

    If I want to pick up files that does not finish with ".xxx" do I use regexp=".*[\^.xxx]$"

    Thanks.
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by borthouth
    Hi,

    I have multiple files with different filenames and want to test out regular expressions.

    I can use regexp=".*" to pick up all files, and also regexp=".*\.xxx $" to pick up all files ending with ".xxx".

    If I want to pick up files that does not finish with ".xxx" do I use regexp=".*[\^.xxx]$"

    Thanks.
    The problem,here, is that you used the escape ('\') which makes the inverse set ('^') a literal. It should be:[CODE=python]>>> import re
    >>> patt = ".*[^.xxx]$"
    >>> fname = 'hello.abc'
    >>> re.match(patt, fname)
    <_sre.SRE_Mat ch object at 0x03262100>
    >>> patt = ".*[^.abc]$"
    >>> re.match(patt, fname)
    >>> None[/CODE]

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Originally posted by borthouth
      Hi,

      I have multiple files with different filenames and want to test out regular expressions.

      I can use regexp=".*" to pick up all files, and also regexp=".*\.xxx $" to pick up all files ending with ".xxx".

      If I want to pick up files that does not finish with ".xxx" do I use regexp=".*[\^.xxx]$"

      Thanks.
      This seems to work:[code=Python]patt = re.compile(r'.+ \.(?!xxx)')[/code]

      Comment

      • ghostdog74
        Recognized Expert Contributor
        • Apr 2006
        • 511

        #4
        i certainly hope you are just testing regexp, because if you really want to get files that doesn't end with .xxx, you can use the endswith() method.

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by ghostdog74
          i certainly hope you are just testing regexp, because if you really want to get files that doesn't end with .xxx, you can use the endswith() method.
          That's worth a million, GD. Just one of the reasons that I am so glad that you decide to pop in every now-and-again. Thanks!

          Comment

          Working...