0

I have a string of different emails

ex: "email1@uy.com, email2@iu.it, email3@uu.edu" etc, etc

I would like to formulate a Regex that creates the following output

ex: "email1,email2,email3" etc, etc

How can I remove characters between an "@" and "," but leaving a "," and a Space in C#

Thank you so much for the help!!

3 Answers3

0

Replacing all occurrences of @[^,]+ with an empty string will do the job.

The expression matches sequences that start in @, inclusive, up to a comma or to the end, exclusive. Therefore, commas in the original string of e-mails would be kept.

Demo.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

Maybe you don't need to use a regex, in that case you can do the following:

string input = "email1@uy.com, email2@iu.it, email3@uu.edu";

 input = input.Replace(" ", "");
 string[] ocurrences = input.Split(',');

 for (int i = 0; i < ocurrences.Length; i++)
 {
     string s = ocurrences[i];
     ocurrences[i] = s.Substring(0, s.IndexOf('@'));
 }

 string final = string.Join(", ", occurences);
Fabio
  • 11,892
  • 1
  • 25
  • 41
0

If you want to replace all characters between @ and comma by blank, the easiest option is to use Regex.Replace:

var emails = "a@m.com, b@m.com, d@m.com";
var result = Regex.Replace(emails, "@[^,]+", string.Empty);
// result is "a, b, d"

Please note that it leaves spaces after comma in the result, as you wanted in your question, though your example result has spaces removed.

The regular expression looks for all substrings starting '@' characters, followed by any character which is not comma. Those substrings are replaced with empty string.

Alexey
  • 1,299
  • 11
  • 10