contactmatt
contactmatt

Reputation: 18600

.NET Regex - Help with regex 'alternatives' (Pipe symbol | )

I have a regex expression that I'm trying to construct that processes a file path and tries to find a file path whose directory ends with "Processed" or "Failed".

I have something like this...

        static string PROCESSED_DIRECTORY = "Processed";
        static string FAILURE_DIRECTORY = "Failed";

...
    if (Regex.IsMatch(FileFullPath, String.Format(@"(.*)\\({0})|({1})$", PROCESSED_DIRECTORY, FAILURE_DIRECTORY)))....

This works fine.

However, I created an additional Regex expression because I am also trying to match the occurance of a file that is located in the Processed or Failed directory. The regex is not matching and I believe it has something to do with the pipe symbol. It matches when I check for either 'Failed' or 'Processed' without the pipe symbol.

For example: The following files don't match - C:\ftp\business\Processed\file.txt - C:\ftp|business\Failed\file.txt

I would expect them to match.

if (Regex.IsMatch(FileFullPath, String.Format(@"(.*)\\({0}|{1})\\(.*)", PROCESSED_DIRECTORY, FAILURE_DIRECTORY)))

If I somehow could combine the two Regex queries into one to say "Match a path that ends with Failed' or 'Processed' and also match a file that exists in the 'Failed' or 'Processed' directory", that'd be amazing. Right now though, I'm content with having two separate regex calls and getting the second to work.

Upvotes: 0

Views: 868

Answers (2)

Justin Morgan
Justin Morgan

Reputation: 30715

There are quite a few ways to do this (progressively more complicated and more correct), but the simplest is probably this:

var targetStrings = new[] { PROCESSED_DIRECTORY, FAILURE_DIRECTORY }; //needles
string FileFullPath = @"C:\ftp\business\Processed\moof\file.txt"; //haystack

if (FileFullPath.Split('\\').Any(str => targetStrings.Contains(str)))
{
    //...

No regex required. Regex seems overkill and possibly error-prone for this anyway.

System.IO.Path may be relevant to whatever you're doing here; it's what the Path class was made for.

Upvotes: 0

Pete M
Pete M

Reputation: 2048

Works ok for me... Ran this in LINQPad:

string PROCESSED_DIRECTORY = "Processed";
string FAILURE_DIRECTORY = "Failed";

string FileFullPath = @"C:\ftp\business\Processed\moof\file.txt";
Regex.IsMatch(FileFullPath, String.Format(@"(.*)\\({0}|{1})\\(.*)", PROCESSED_DIRECTORY, FAILURE_DIRECTORY)).Dump();


FileFullPath = @"C:\ftp|business\Failed\file.txt";
Regex.IsMatch(FileFullPath, String.Format(@"(.*)\\({0}|{1})\\(.*)", PROCESSED_DIRECTORY, FAILURE_DIRECTORY)).Dump();

Here's a version that will look for either containing the processed/failed strings OR ending in \Processed|Failed\filename.ext:

string PROCESSED_DIRECTORY = "ProcessedPath";
string FAILURE_DIRECTORY = "FailedPath";

string FileFullPath = @"C:\ftp\business\ProcessedPath\moof\file.txt";
Regex.IsMatch(FileFullPath, String.Format(@"((.*)\\({0}|{1})\\(.*))|(.*\\(Processed|Failed)\\(?!.*\\.*))", PROCESSED_DIRECTORY, FAILURE_DIRECTORY)).Dump();

FileFullPath = @"C:\ftp\business\NotTheProcessedPath\moof\Processed\file.txt";
Regex.IsMatch(FileFullPath, String.Format(@"((.*)\\({0}|{1})\\(.*))|(.*\\(Processed|Failed)\\(?!.*\\.*))", PROCESSED_DIRECTORY, FAILURE_DIRECTORY)).Dump();

Upvotes: 1

Related Questions