Home > Enterprise >  Regex Conditional C# checking two string values
Regex Conditional C# checking two string values

Time:01-10

I have two files and need to validate only one filenames at a time .

Regex filter = new Regex("P[01][123456789]REM3_" year "Test_English" ".txt"); Regex filter = new Regex("P[01][123456789]REM3" year "_Test_French" ".txt")

I need to check either one file for every condition. Is there any regex condition which can handle OR condition.

string test="P[01][123456789]REM3_" year "Test_English" ".txt"; string test1="P[01][123456789]REM3" year "_Test_French" ".txt";

I tried this condition but i run through errors if (filter ==test |test1)

CodePudding user response:

You can use the | character, which is known as the "pipe" character and work as "OR" operator, it allows you to specify multiple possible patterns to match:

Regex filter = new Regex("P[01][123456789]REM3_"   year   "Test_English"  ".txt"   "|"   "P[01][123456789]REM3"   year   "_Test_French"  ".txt");

then check like this:

if (filter.IsMatch(someString))
{
    // The string someString matches the regular expression
}
else
{
    // The string someString does not match the regular expression
}
  • Related