0

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 and store it in int<list> or in datatable with expected output as below

3167763
3195330

i have tried with below code but its working only when the ""Error importing username:" text appear once but in my case that text is reapeating and i need to get all the username in a list

Your help is very much needed ,Thanks in Advance

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\"}";
string strStart = "Error importing username:";
string strEnd = ",";


if (error_msg.Contains(strStart) && error_msg.Contains(strEnd))
{
                                    
    int Start = error_msg.IndexOf(strStart) + strStart.Length;
    int End = error_msg.IndexOf(strEnd, Start);
    return error_msg.Substring(Start, End - Start);
}
Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35
7M views
  • 11
  • 2
  • If you use IndexOf instead of Contains, you can specify where the search should start - if you use the end of the last occurrence found, you can keep repeating the search until you find all occurrences. Or of course, just use Regex. – Luaan Nov 04 '20 at 13:08

2 Answers2

1

The simplest solution would be Regex:

List<int> list = Regex.Matches(error_message, @"(?<=Error importing username: )\d+")
    .Select(match => int.Parse(match.Value))
    .ToList();

(?<=Error importing username: ) is a positive look-behind that detects the text Error importing username: but does not include it in the match.

\d+ matches one or more digits.

Working example


Update

In .Net Framework, MatchCollection does not implement IEnumerable<Match> , so a Cast<Match>() is required:

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

Working example

Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35
  • nice! much better than mine – devcrp Nov 04 '20 at 13:17
  • i am getting below error when using ur code , could u pls help Error CS1061 'MatchCollection' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'MatchCollection' could be found (are you missing a using directive or an assembly reference?) i have used all required below namespace still getting this error using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; – 7M views Nov 04 '20 at 13:23
  • @7Mviews Probably because you're using .Net Framework rather than .Net Core. See my update. – Johnathan Barclay Nov 04 '20 at 13:29
  • along with username how can i fetch the corresponding primary email: value for Example "Error importing username: 3167763, primary email: pkumar194@google.com" for this i need the output as collection with user id and email in seperate columns 3167763 pkumar194@google.com i tried different way but no luck, your help is very much appreciated – 7M views Nov 11 '20 at 07:37
  • @7Mviews You could use Regex capturing groups, but you should really post this as a new question because the answer would be significantly different to the above. – Johnathan Barclay Nov 11 '20 at 09:52
  • @JohnathanBarclay could you able to help me on this https://stackoverflow.com/questions/64965806/find-specific-text-and-coresponding-email-id-from-a-middle-of-a-string-and-store – 7M views Nov 23 '20 at 09:34
0

Well I think this should do it using linq. This is assuming the string Error importing username: is in the error_message. Of course if the format of the input string changes it'd need some tweaking.

var ids = error_message.Split("Error importing username: ")
                .Skip(1)
                .Select(x => x.Split(",").First());
Dharman
  • 30,962
  • 25
  • 85
  • 135
devcrp
  • 1,323
  • 7
  • 17