0

I have a list of paths, and I need a match for all starting with C:\Program Files\_folder_\_folder_\etc and then having any amount and any type of character a windows path can use. This is how I approach, though I guess this only allows alpanumeric characters:

string pathPattern = @"C:\Program Files\_folder_\_folder_\etc[0-z]*";
if (System.Text.RegularExpressions.Regex.IsMatch(myList[n], pathPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
    {
        //some action
    }
fishmong3r
  • 1,414
  • 4
  • 24
  • 51

3 Answers3

2

If C:\Program Files\_folder_\_folder_\etc is the pre-defined part. I think you can do this without Regex like this using Contains

if(pathPattern.Contains(@"C:\Program Files\_folder_\_folder_\etc"))
{
    //some action
} 

Edit 1: You can do ToLower() on pathPattern

var ss = pathPattern.ToLower().Contains(@"C:\Program Files\_folder_\_folder_\etc".ToLower());
ElectricRouge
  • 1,239
  • 3
  • 22
  • 33
  • 1
    Thank you again. I also found this regarding the case sensitivity issue: http://stackoverflow.com/questions/444798/case-insensitive-containsstring – fishmong3r Mar 20 '14 at 12:45
1

1. If you want your path should starts with :

C:\Program Files\

then use below code :

if (Regex.IsMatch(code, @"[cC]{1}\:\\Program Files\\.*"))
{
   \\ Do Your work
}

2. If you want your path should starts with :

c:\Program Files_folder__folder_\

then use below code :

if (Regex.IsMatch(code, @"[cC]{1}\:\\Program Files\\_folder_\\_folder_\\.*"))
{
    \\ Do Your work
}

Description:

  1. cC{1} : Check for C drive (c:)
  2. @"[cC]{1}\:\Program Files\ : Check for C:\Program Files
  3. .* : Checks for any folder after C:\Program Files
SpiderCode
  • 10,062
  • 2
  • 22
  • 42
0

Try this regex

^(?:[a-zA-Z]\:|\\\\[\w\.]+\\[\w.$]+)\\(?:[\w]+\\)*\w([\w.])+$

Sample Code

string path = @"C:\Program Files\_folder_\_folder_\";
string pathPattern = @"^(?:[a-zA-Z]\:|\\\\[\w\.]+\\[\w.$]+)\\(?:[\w]+\\)*\w([\w.])+$"

if(System.Text.RegularExpressions.Regex.IsMatch(path, pathPattern)){
    //It matches the regex
}
else
{
    //it doesnot matches the regex
}

Works for these:

\\test\test$\TEST.xls
\\server\share\folder\myfile.txt
\\server\share\myfile.txt
\\123.123.123.123\share\folder\myfile.txt
c:\folder\myfile.txt
c:\folder\myfileWithoutExtension
Pratik Bhoir
  • 2,074
  • 7
  • 19
  • 36
  • 3
    could you explain this regex..? As someone who is not a regex expert I would love to see what it actually does – default Mar 20 '14 at 10:35
  • Yes please let us know what it does, and which characters does it match exactly, and how to merge this with the pre-defined part of path in the begining. – fishmong3r Mar 20 '14 at 10:48
  • You don't understand what I want... The path can be literally anything, and the patter should be 'C:\Program Files\_folder_\_folder_\+AnyCharacter' – fishmong3r Mar 20 '14 at 11:02
  • So you want to match all the list with the pattern and return true if all of them matches and return false if not. Is is something like that – Pratik Bhoir Mar 20 '14 at 11:14