开发者

.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 additiona开发者_JAVA百科l 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.


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();


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜