Search code examples
c#.netregexcassette

Checking text against several options


I'm very rusty on my regex and have spent about 2 hours trying to do something which should be very simple.

Basically, I want a regex expression that can check a bunch of file names (the end goal being to exclude the ones not relevant - this is for using the FileSearch class in Cassette to exclude unnecessary javascipt files).

The expression I have so far is:

(^.+\.+min.js$) | (^Microsoft.$) | (^.+\.+min.js$) | (^.+vsdoc.js$)

and the text (filename) I am trying to match is

jquery-ui-i18n.min.js

This works in Espresso, but when I actually run it against some .NET code, there is no match:

    static void Main(string[] args)
{
  System.Text.RegularExpressions.Regex reg =
    new System.Text.RegularExpressions.Regex(@"(^.+\.+min.js$) | (^Microsoft.$) | (^.+\.+min.js$) | (^.+vsdoc.js$)", System.Text.RegularExpressions.RegexOptions.None);

  if (reg.IsMatch("jquery-ui-i18n.min.js"))
    Console.WriteLine("match");
  else
    Console.WriteLine("no match");

  Console.Read();
}

Are there any regex legends out there who can show me the light!?


Solution

  • I would use

    (\.min\.js|Microsoft.|\.min\.js|vsdoc\.js)$
    

    You regex has many faults

    (^.+\.+min.js$) | (^Microsoft.$) | (^.+\.+min.js$) | (^.+vsdoc.js$)
     -           -   -                      
     |           |   |->space would make ^ meaningless unless you use IgnorePatternWhiteSpace option 
     |           |->instead of repeating $ in each group you can keep it outside all the groups
     |->^ is not required because you only want to check the end