Find Specific Text and coresponding email id from a middle of a string and store it into datatable or in List in C#

狂风中的少年 提交于 2020-12-13 03:15:12

问题


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();

回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/64965806/find-specific-text-and-coresponding-email-id-from-a-middle-of-a-string-and-store

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!