question

FrankMehlhop-8022 avatar image
0 Votes"
FrankMehlhop-8022 asked ZhiLv-MSFT answered

Regex for a search with exceptions

I'm looking for a regex expression in .Net environment.
I want to get all items including "Hello",
but not when on the same item is also "Paul" or "Mary".

How does it look like?

Frank

dotnet-aspnet-core-webapi
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @FrankMehlhop-8022,

but not when on the same item is also "Paul" or "Mary".

What's the processing text looks like? Do you mean the processing text contains "Hello Paul" or "Hello Mary", or another person's name, and you want to find all of them?



0 Votes 0 ·
FrankMehlhop-8022 avatar image
0 Votes"
FrankMehlhop-8022 answered

Let's say I have following items:

  • Hello Joe

  • Hello Mary

  • Hello Bill

  • How are you

  • It's a nice day

I like to get all lines / items with "Hello" except this lines where is a "Mary" or "Paul".
So my desired outcome would be:

  • Hello Joe

  • Hello Bill


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ZhiLv-MSFT avatar image
0 Votes"
ZhiLv-MSFT answered

Hi @FrankMehlhop-8022,

I assume you are using a string list to store the items, if that is the case, you could refer the following sample code:

         List<string> stritems = new List<string>()
         {
             "Hello Joe",
             "Hello Mary",
             "Hello Bill",
             "How are you",
             "It's a nice day",
         };
         //filter the items start with Hello
         Regex rx = new Regex(@"\bHello\s+(\w+?)\b", RegexOptions.Compiled | RegexOptions.IgnoreCase);

         //filter the item with Hello Mary and Hello Paul.
         Regex rx2 = new Regex(@"\bHello\s+(Mary)|(Paul)\b", RegexOptions.Compiled | RegexOptions.IgnoreCase);

         //used to store the result.
         List<string> result = new List<string>();

         //loop through the items and filter data.
         foreach(var item in stritems)
         {
             if (rx.IsMatch(item) && !rx2.IsMatch(item))
             {
                 result.Add(item);//add the matched items to the result.
             }

         }

         //another method: Using Linq query statement https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/
         var namelist = new List<string>() { "Mary", "Paul" }; 
         var result2 = stritems.Where(c => c.StartsWith("Hello") && !namelist.Any(d => c.Contains(d))).ToList();

The result as below:

145746-2.gif


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Best regards,
Dillion


2.gif (160.5 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.