Using Negative Lookahead in Regex Match


I am using System.Text.RegularExpressions.Regex.IsMatch to extract a file name. My aim is to count the number of files with names which contain the word CDA and NDA but not Edited. I have the code here:

(Directory.GetFiles(Contract.ToString,"",SearchOption.AllDirectories)).ToArray.Where(function(x) System.Text.RegularExpressions.Regex.IsMatch(x,/^(?!.“Edited”+$)(“NDA|CDA”),RegexOptions.IgnoreCase)).Count following the test on regex (which worked well)

However, the code is not working at the moment after I added the negative lookahead in. The error says “Expression expected. End of expression expected”. Is there anyway I can fix this?

Thank you

@upnewbie Can you check the expression again, you might be missing a bracket .

1 Like

@upnewbie Try this Expression :

Directory.GetFiles(Contract.ToString,"",SearchOption.AllDirectories).ToArray.Where(function(x) System.Text.RegularExpressions.Regex.IsMatch(x,"^(?!.*Edited+$)(NDA|CDA)")).Count


Thank you so much, it worked :slight_smile:

1 Like

Hi sorry, I am having a problem with it again as now it will not return anything after adding the negative lookahead in. I thought it worked but turnt out it failed :slight_smile:

@upnewbie Can you give more details as to which inputs it didn’t work

Hi, after adding this in (?!.*Edited+$) it didnt return anything back anymore. (NDA|CDA) by itself still works perfectly well

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.