locked
Regular expression for few multiple line from email body RRS feed

  • Question

  • User-449737870 posted

    Hi,

    I want to create a regular expression for email body. my requirement is as below

    suppose i have the e-mail body like bellow:

    Hi,

    this is line 1

    this is line 2

    this is line 3

    this is line 4

    this is line 5

    this is line 6

    Regards,

    XYZ Team,

    I want to search two or more different line e.g this is line 2 and this is line 5  using regular expression

    So what will be the regular expression for that kindly help me out.

    Thursday, April 5, 2018 11:08 AM

Answers

  • User-1838255255 posted

    Hi Zishan Ansari,

    According to your description and code, i make a modify based on community member a2h's code, please check: 

    Sample code: 

    int matches = 0;
    string yourEmailBody = @"Hi,
                           this is line 1
                           this is line 2
                           this is line 3
                           this is line 4
                           this is line 5
                           this is line 6
                           Regards,
                           XYZ Team,";
    var allsearchPhare = "this is line 8,this is line 3"; string[] newsearch = allsearchPhare.Split(','); for (int i = 0; i < newsearch.Length; i++) { string MatchPhrase = "(?=(" + newsearch[i].ToLower() + "))"; int count = Regex.Matches(yourEmailBody, MatchPhrase).Count; if (count == 1) { matches++; } } Response.Write("Match Lines Count is: " + matches);

    Best Regards,

    Eric Du 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 10, 2018 9:46 AM
  • User-449737870 posted

    I found perfect solution as below:

    static void Main(string[] args)
            {
                int matches = 0;
                string yourEmailBody = @"Hi,
                           this is line 1
                           this is line 2
                           this is line 3
                           this is line 4
                           this is line 5
                           this is line 6
                           Regards,
                           XYZ Team,";
    
                var allsearchPhare = "this is line 5,this is line 3";
                string[] newsearch = allsearchPhare.Split(',');
    
                for (int i = 0; i < newsearch.Length; i++)
                {
                    string MatchPhrase = "(?=(" + newsearch[i].ToLower() + "))";
                    int count = Regex.Matches(yourEmailBody, MatchPhrase).Count;
                    if (count >= 1)
                    {
                        matches++;
                    }
                }
                if (matches == newsearch.Length)
                    Console.WriteLine("All Matches Found");
    
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 10, 2018 11:29 AM

All replies

  • User2103319870 posted

    So what will be the regular expression for that kindly help me out.

    You can try with below regex

    string yourEmailBody = @"Hi,
                                            this is line 1
                                            this is line 2
                                            this is line 3
                                            this is line 4
                                            this is line 5
                                            this is line 6
                                            Regards,
                                            XYZ Team,";
                string searchPhrase = "this is line 5";
                string MatchPhrase = "(?=(" + searchPhrase.ToLower() + "))";
                int matches = Regex.Matches(yourEmailBody, MatchPhrase).Count;
    Thursday, April 5, 2018 1:14 PM
  • User-449737870 posted

    Thanks A2H,

    But the searchPhrase will contain atleast two lines e.g it will contain this is line 5, this is line 2 in two different place.

    Thursday, April 5, 2018 2:51 PM
  • User-1838255255 posted

    Hi Zishan Ansari,

    According to your description and code, i make a modify based on community member a2h's code, please check: 

    Sample code: 

    int matches = 0;
    string yourEmailBody = @"Hi,
                           this is line 1
                           this is line 2
                           this is line 3
                           this is line 4
                           this is line 5
                           this is line 6
                           Regards,
                           XYZ Team,";
    var allsearchPhare = "this is line 8,this is line 3"; string[] newsearch = allsearchPhare.Split(','); for (int i = 0; i < newsearch.Length; i++) { string MatchPhrase = "(?=(" + newsearch[i].ToLower() + "))"; int count = Regex.Matches(yourEmailBody, MatchPhrase).Count; if (count == 1) { matches++; } } Response.Write("Match Lines Count is: " + matches);

    Best Regards,

    Eric Du 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 10, 2018 9:46 AM
  • User-449737870 posted

    I found perfect solution as below:

    static void Main(string[] args)
            {
                int matches = 0;
                string yourEmailBody = @"Hi,
                           this is line 1
                           this is line 2
                           this is line 3
                           this is line 4
                           this is line 5
                           this is line 6
                           Regards,
                           XYZ Team,";
    
                var allsearchPhare = "this is line 5,this is line 3";
                string[] newsearch = allsearchPhare.Split(',');
    
                for (int i = 0; i < newsearch.Length; i++)
                {
                    string MatchPhrase = "(?=(" + newsearch[i].ToLower() + "))";
                    int count = Regex.Matches(yourEmailBody, MatchPhrase).Count;
                    if (count >= 1)
                    {
                        matches++;
                    }
                }
                if (matches == newsearch.Length)
                    Console.WriteLine("All Matches Found");
    
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 10, 2018 11:29 AM