locked
Extract all email addresses from a plain text using C# and insert on different tables database RRS feed

  • Question

  • User1151703306 posted

    Hi,

    Is there a way to extract all email addresses from a plain text using C#.

    For example this string:

    [id=4068;name=mrgar@yahoo.com]
    [id=4078;name=mrrame@gmail.com]
    [id=4088;name=mrgio@yahoo.com]

    return

    mrgar@yahoo.com, mrrame@gmail.com, mrgio@yahoo.com

    I need insert into differents MySQL table the string.

    First mail address into dotable_1 and all other mail address into dotable_2.

    On this example:

    1. mrgar@yahoo.com,  insert into dotable_1;
    2. mrrame@gmail.com, mrgio@yahoo.com , insert into dotable_2


    How to do this?

    My code below.

            protected void btnFinal_Click(object sender, EventArgs e)
            {
                string exampleTxt = @"[id=4068;name=mrgar@yahoo.com][id=4078;name=mrrame@gmail.com][id=4088;name=mrgio@yahoo.com]";
                string[] emails = identifyEmailAddress(exampleTxt);
                foreach(string s in emails)
                {
                    displayLabel.Text += s + "<br />";
                }
            }
    
            // Return emails with a string array.
            private string[] identifyEmailAddress(string txt)
            {
                const string MatchEmailPattern =@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"; 
                Regex rx = new Regex(MatchEmailPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                MatchCollection matches = rx.Matches(txt);
    
                int noOfMatches = matches.Count;
    
                string[] result = new string[noOfMatches];
    
                int i = 0;
                foreach (Match match in matches)
                {
                    result[i++] = match.Value.ToString();
                }
    
                return result;
            }

    Wednesday, May 13, 2020 8:00 AM

Answers

  • User475983607 posted

    The code I posted is tested and verified.  You must be using an older version of C#.  

    What version .NET framework are you targeting?

    Anyway, the changes are most likely trivial.

    string value = "mrgar@yahoo.com; mrgio@yahoo.com; mrrame@gmail.com";
    
    List<string> values = value.Split(';').ToList();
    string Table1 = values[0];
    values.RemoveAt(0);
    string Table2 = string.Join(";", values).Trim();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 14, 2020 2:46 PM

All replies

  • User197322208 posted

    if( emails.length >0){

    // write EF code / Sql to insert emails[0] into dotable_1;

     for(i=1; i<emails.length-1; i++){

    // write EF code / Sql to insert emails[i] into dotable_2;

    }

    }

    Wednesday, May 13, 2020 10:41 AM
  • User1151703306 posted

    if( emails.length >0){

    // write EF code / Sql to insert emails[0] into dotable_1;

     for(i=1; i<emails.length-1; i++){

    // write EF code / Sql to insert emails[i] into dotable_2;

    }

    }

    Hi,

    thanks for help.

    but I have error

    Specified argument was out of the range of valid values. Parameter name: i
    

    on this line

    for (i = 1; i < matches[i].Length - 1; i++)

    my code below.

                        Movie[] listMovie = jsSer.ConvertToType<Movie[]>(obj);
                        foreach (Movie p in listMovie)
                        {
                            string pattern = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
                            Regex re = new Regex(pattern);
                            
                            if (p.ToString() != null)
                            {
                                MatchCollection matches = re.Matches(p.ToString());
    
                                if (matches.Count > 0)
                                {
                                    for (int i = 0; i < matches.Count; i++)
                                    {
                                        Response.Write(matches[i] + ";<br />");
    
                                        if (matches[i].Length > 0)
                                        {
                                           Response.Write(matches[i] + ";<br />");
    
                                            for (i = 1; i < matches[i].Length - 1; i++)
                                            {
                                                Response.Write(matches[i] + ";<br />");
                                            }
                                        }
                                   }
                                }
                            }

    Thursday, May 14, 2020 6:41 AM
  • User753101303 posted

    Hi,

    You considered removing brackets, using String.Split'(line,";") to have  id=x and name=y as array cells and and then remove the prefix ? When the "grammar" is simple enough I favor NOT using regular expressions.

    Not directly related and you likely have no control on the input format, but using JSON would have been better. If it can be planned at a later time I would perhaps "fix" the string so that it is valid json.

    Thursday, May 14, 2020 7:03 AM
  • User1151703306 posted

    Hi,

    You considered removing brackets, using String.Split'(line,";") to have  id=x and name=y as array cells and and then remove the prefix ? When the "grammar" is simple enough I favor NOT using regular expressions.

    Not directly related and you likely have no control on the input format, but using JSON would have been better. If it can be planned at a later time I would perhaps "fix" the string so that it is valid json.

    Hi PatriceSc,
    thanks for reply.

    the input format of JSON is valid

    Thursday, May 14, 2020 8:25 AM
  • User753101303 posted

    Do you mean you solved your problem since then? What you are showing now seems unrelated to what you shown first which is not valid JSON:

    [id=4068;name=mrgar@yahoo.com]
    [id=4078;name=mrrame@gmail.com]
    [id=4088;name=mrgio@yahoo.com]

    If you can, use JSON rather than your own custom format. If it is your intent, avoid to generate JSON "by hand" and use available tools (possibly JSON.stringify on the browser side if not done already by your Ajax library). Unclear if you need further help or if your problem is solved.

    Thursday, May 14, 2020 8:50 AM
  • User1151703306 posted

    Do you mean you solved your problem since then? What you are showing now seems unrelated to what you shown first which is not valid JSON:

    [id=4068;name=mrgar@yahoo.com]
    [id=4078;name=mrrame@gmail.com]
    [id=4088;name=mrgio@yahoo.com]

    If you can, use JSON rather than your own custom format. If it is your intent, avoid to generate JSON "by hand" and use available tools (possibly JSON.stringify on the browser side if not done already by your Ajax library). Unclear if you need further help or if your problem is solved.

    No, my problem is unresolved.

    the string:

    mrgar@yahoo.com, mrrame@gmail.com, mrgio@yahoo.com

    I need insert into differents MySQL table the string.

    First mail address into dotable_1 and all other mail address into dotable_2.

    On this string:

    1. mrgar@yahoo.com, >>>> insert into dotable_1;
    2. mrrame@gmail.com, mrgio@yahoo.com , >>>> insert into dotable_2
    Thursday, May 14, 2020 9:01 AM
  • User475983607 posted

    You responses are contradictory! 

    It sounds like a design issue.  Use standard programming practices rather than making up your own.   Either format the data as JSON and submit the JSON.

    [
      {
        "id": "4068",
        "name: "mrgar@yahoo.com"
      },
      {
        "id": "4078",
        "name": "mrrame@gmail.com"
      },
      {
        "id": "4088",
        "name": "mrgio@yahoo.com"
      }
    ]

    Or use HTML forms (application/x-www-form-urlencoded)

    id=4068&name=mrgar@yahoo.com&id=4078&name=mrrame@gmail.com&id=4088&name=mrgio@yahoo.com

    It's not clear how you are going from JSON to the format sown in your original post. 

    [id=4068;name=mrgar@yahoo.com]
    [id=4078;name=mrrame@gmail.com]
    [id=4088;name=mrgio@yahoo.com]

    Share your source code if you need assistance but it seems yo need to rethink the design.

    Thursday, May 14, 2020 10:49 AM
  • User1151703306 posted

    You responses are contradictory! 

    Contradictory?

    what is not clear in the question?

    My string :

    it would be possible to split this string to two elements :

    mrgar@yahoo.com;

    and

     mrgio@yahoo.com; mrrame@gmail.com;

    For insert 

    mrgar@yahoo.com;

    Into table_1 ?

    and insert 

     mrgio@yahoo.com; mrrame@gmail.com;

    into table_2 ?

    the number of element of part one will always be one

    the number of elements of part two is variable, it could be one but also 10/100/500

    is it clearer now?

    what should i say more?

    thanks

    Golia

    Thursday, May 14, 2020 1:36 PM
  • User475983607 posted

    Golia

    Contradictory?

    what is not clear in the question?

    Well, you keep changing the the code. Initially the format was...

    [id=4068;name=mrgar@yahoo.com]
    [id=4078;name=mrrame@gmail.com]
    [id=4088;name=mrgio@yahoo.com]

    Next, you stated you are using JSON

    [
      {
        "id": "4068",
        "name: "mrgar@yahoo.com"
      },
      {
        "id": "4078",
        "name": "mrrame@gmail.com"
      },
      {
        "id": "4088",
        "name": "mrgio@yahoo.com"
      }
    ]

    Now you have a semi-colon separated string....  A moving target.

    mrgar@yahoo.com; mrgio@yahoo.com; mrrame@gmail.com

    IMHO, you have design issues but are not addressing the design.  You keep making changes to the code without explaining the changes to the community.   My assumption is your DB has issues.

    Anyway, this latest requirement  is trivial. 

    string value = "mrgar@yahoo.com; mrgio@yahoo.com; mrrame@gmail.com";
    
    List<string> values = value.Split(';', StringSplitOptions.RemoveEmptyEntries).ToList();
    string Table1 = values[0];
    values.RemoveAt(0);
    string Table2 = string.Join(';', values).Trim();
    
    Console.WriteLine(Table1);
    Console.WriteLine(Table2);

    Results

    mrgar@yahoo.com
    mrgio@yahoo.com; mrrame@gmail.com



      

    Thursday, May 14, 2020 2:05 PM
  • User1151703306 posted

    thanks but 

    CS1503: Argument 2: cannot convert from 'System.StringSplitOptions' to 'char'

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string value = "mrgar@yahoo.com; mrgio@yahoo.com; mrrame@gmail.com";
    
                List<string> values = value.Split(';', StringSplitOptions.RemoveEmptyEntries).ToList();
                string Table1 = values[0];
                values.RemoveAt(0);
                string Table2 = string.Join(';', values).Trim();
    
                Console.WriteLine(Table1);
                Console.WriteLine(Table2);
            }
        }

    Thursday, May 14, 2020 2:29 PM
  • User475983607 posted

    The code I posted is tested and verified.  You must be using an older version of C#.  

    What version .NET framework are you targeting?

    Anyway, the changes are most likely trivial.

    string value = "mrgar@yahoo.com; mrgio@yahoo.com; mrrame@gmail.com";
    
    List<string> values = value.Split(';').ToList();
    string Table1 = values[0];
    values.RemoveAt(0);
    string Table2 = string.Join(";", values).Trim();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 14, 2020 2:46 PM
  • User1151703306 posted

    thank you now working

    version .NET framework 4.7.2

    Thursday, May 14, 2020 3:18 PM