0

Let's say that I have a string that has the following:

string mystr = "*myid*-myemail-:mypwd:*myid2*-myemail2-:mypwd2:*myid3*-myemail3-:mypwd3:";

and I want to get each of the substrings that have the'*' and then add them to a new string and then get each of the values that have the ':' same with the one that has '-', and so I tried this using the split method, but it does not work.

string mystr = "*myid*-myemail-:mypwd:*myid2*-myemail2-:mypwd2:*myid3*-myemail3-:mypwd3:";

string[] myid = mystr.Split('*');
string[] myemail = mystr.Split('-');
string[] mypwd = mystr.Split(':');
string ids = "";
string emails = "";
string pwds = "";

 foreach (string k in myid)
{
    Console.WriteLine("THESE ARE THE IDS");
    Console.WriteLine(k);
}
foreach (string k in myemail)
{
    Console.WriteLine("THESE ARE THE EMAILS");
    Console.WriteLine(k);
}
foreach (string k in mypwd)
{
    Console.WriteLine("THESE ARE THE PASSWORDS");
    Console.WriteLine(k);
}

Here is the console output:

THESE ARE THE IDS

THESE ARE THE IDS
myid
THESE ARE THE IDS
-myemail-:mypwd:
THESE ARE THE IDS
myid2
THESE ARE THE IDS
-myemail2-:mypwd2:
THESE ARE THE IDS
myid3
THESE ARE THE IDS
-myemail3-:mypwd3:
THESE ARE THE EMAILS
*myid*
THESE ARE THE EMAILS
myemail
THESE ARE THE EMAILS
:mypwd:*myid2*
THESE ARE THE EMAILS
myemail2
THESE ARE THE EMAILS
:mypwd2:*myid3*
THESE ARE THE EMAILS
myemail3
THESE ARE THE EMAILS
:mypwd3:
THESE ARE THE PASSWORDS
*myid*-myemail-
THESE ARE THE PASSWORDS
mypwd
THESE ARE THE PASSWORDS
*myid2*-myemail2-
THESE ARE THE PASSWORDS
mypwd2
THESE ARE THE PASSWORDS
*myid3*-myemail3-
THESE ARE THE PASSWORDS
mypwd3
THESE ARE THE PASSWORDS

And this is what I want it to look like:

THESE ARE THE PASSWORDS
mypwd
mypwd2
mypwd3

 THESE ARE THE EMAILs
myemail
myemail2
myemail3

THESE ARE THE IDS
myid
myid2
myid3
9
  • but it does not work How specifically does it not work? Commented Jul 17, 2021 at 0:17
  • well this is the output of the console myid -myemail-:mypwd: myid2 -myemail2-:mypwd2: myid3 -myemail3-:mypwd3: myid myemail :mypwd:*myid2* myemail2 :mypwd2:*myid3* myemail3 :mypwd3: myid-myemail- mypwd myid2-myemail2- mypwd2 myid3-myemail3- mypwd3 Commented Jul 17, 2021 at 0:22
  • 2
    Put the output in the question you are setting and the output you want instead. Commented Jul 17, 2021 at 0:24
  • I assume that your mystr actually contains real email addresses & passwords? Commented Jul 17, 2021 at 0:29
  • 1
    @cipher_449449 - Please note that the real-world data would significantly alter how to answer this question. You should be very clear on what characters can appear in your email and/or passwords. Commented Jul 17, 2021 at 1:26

1 Answer 1

2

You may use Regular Expressions for this:

string mystr = "*myid*-myemail-:mypwd:*myid2*-myemail2-:mypwd2:*myid3*-myemail3-:mypwd3:";
var matches = Regex.Matches(mystr, @"\*(?<Id>[^*]+)\*-(?<Email>[^-]+)-:(?<Password>[^:]+):");

Then, you can either access the matches as follows:

foreach (Match match in matches)
{
    Console.WriteLine("Id=" + match.Groups["Id"].Value);
    Console.WriteLine("Email=" + match.Groups["Email"].Value);
    Console.WriteLine("Password=" + match.Groups["Password"].Value);
}

Or if you want to group the Ids, emails, and passwords together, use:

var ids = matches.OfType<Match>().Select(m => m.Groups["Id"].Value);
var emails = matches.OfType<Match>().Select(m => m.Groups["Email"].Value);
var passwords = matches.OfType<Match>().Select(m => m.Groups["Password"].Value);

foreach (string id in ids)
    Console.WriteLine(id);

foreach (string email in emails)
    Console.WriteLine(email);

foreach (string password in passwords)
    Console.WriteLine(password);

Try it online.

Note, however, that this might break because an email address could contain a hyphen character and the password could contain a colon. In that case, you need to come up with a more robust format for the input text.

Sign up to request clarification or add additional context in comments.

2 Comments

It's fair that this works with the OP's anaemic example string, but it fails quickly with real-world data where the email and/or password contain the splitting characters.
@Enigmativity Yep, I was just adding that as a warning in the answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.