Results 1 to 4 of 4

Thread: [2.0] Regex match if it doesn't end with?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    [2.0] Regex match if it doesn't end with?

    I basically want a regex expression that only matches if the string doesn't end in ".php".

    The only thing I know of would be "[^.php]". But because of how [^x] works, it would also match things ending in "php.", etc.

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [2.0] Regex match if it doesn't end with?

    I think the regex you want is: \.php$ and then just do a not of the IsMatch()

  3. #3
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2.0] Regex match if it doesn't end with?

    Hey,

    Where are you getting the string from? Are you looping through a set of files on the file system? If so, why not use the methods on the Path Class:

    http://msdn.microsoft.com/en-us/libr...h_methods.aspx

    Such as, GetExtension:

    http://msdn.microsoft.com/en-us/libr...extension.aspx

    Just thought I would put it out there as an alternative.

    Hope that helps!!

    Gary

  4. #4
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    Re: [2.0] Regex match if it doesn't end with?

    Both options are very workable To use "pure" regex, a negative look-behind assertion would be what you're after:

    Code:
    Regex t = new Regex(@".*$(?<!\.php)");
    Console.WriteLine(t.IsMatch("myfile.txt") + "  *" + t.Match("myfile.txt").Value + "*");
    Console.WriteLine(t.IsMatch("myfile.php") + "  *" + t.Match("myfile.php").Value + "*");
    Console.WriteLine(t.IsMatch("myfile.php.txt") + "  *" + t.Match("myfile.php.txt").Value + "*");

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width