1

I have a string as below

string error_message= "{\"2705\":\"Error importing username: 3167763, primary email: pkumar194@google.com, error: User already exists but Email does not match: pkumar194@googlee.com vs pkumar193@google.co.in\",\"10001\":\"Error importing username: 3195330, primary email: alejandra.mejia@google.com, error: User already exists but Email does not match: alejandra.mejia@google.com vs alejandra.mejia@googlee.com\"}";

from the above string i need to find the repeating text "Error importing username:" and take the username value next to it along with corresponding email id after the text "primary email:" and store it in in datatable with expected output as below

Expected Result in Datatable as below

username    primary email
3167763     pkumar194@google.com
3195330     alejandra.mejia@google.com

below is the code sample i have where i can able to get all the username in list i need to modify the below code to get both username and corresponding primary email id as well in a collection your help will be very much useful

List<int> list = Regex.Matches(error_message, @"(?<=Error importing username: )\d+")
    .Cast<Match>()
    .Select(match => int.Parse(match.Value))
    .ToList();
7M views
  • 11
  • 2

2 Answers2

0

You could create a dictionary with username as key and email as value:

string pattern = @"Error importing username: (\d+), primary email: ([^,]+)";
var dict = Regex.Matches(error_message, pattern)
    .Cast<Match>()
    .ToDictionary(match => match.Groups[1], match => match.Groups[2]);

The pattern includes 2 capturing groups, denoted by parenthesis, which can be used to extract the username and email from each match.

The email pattern [^,]+ simply matches one or more characters that are not a comma; it does not ensure valid email addresses.

If this is a requirement, more details can be found here.

Working example

Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35
0

Instead of using a single lookbehind, you could use 2 capturing groups.

\bError importing username: (\d+), primary email: ([^\s@]+@[^\s@,]+)

Regex demo | C# demo

For example

string pattern = @"\bError importing username: (\d+), primary email: ([^\s@]+@[^\s@,]+)";
string input = @"string error_message= ""{\""2705\"":\""Error importing username: 3167763, primary email: pkumar194@google.com, error: User already exists but Email does not match: pkumar194@googlee.com vs pkumar193@google.co.in\"",\""10001\"":\""Error importing username: 3195330, primary email: alejandra.mejia@google.com, error: User already exists but Email does not match: alejandra.mejia@google.com vs alejandra.mejia@googlee.com\""}"";";

var collection = Regex.Matches(input, pattern)
.Cast<Match>()
.Select(match => 
new {username = int.Parse(match.Groups[1].Value), primary_email = match.Groups[2].Value}
);

foreach (var item in collection) {
    Console.WriteLine("username: {0}, primary email: {1}", 
    item.username, 
    item.primary_email
    );
}

Output

username: 3167763, primary email: pkumar194@google.com
username: 3195330, primary email: alejandra.mejia@google.com
The fourth bird
  • 154,723
  • 16
  • 55
  • 70