-7

I need to extract the Password value which is in bold (Password10) from the text given below. I am using c# programming language.

FName Lname, your system password was changed. If you did not change it or do not know why it was changed, you should contact an administrator immediately.Your new password is Password10

If you have any questions, please contact:

Solutions Program Office Phone: Email: notifications@cho.com

Thank you for using xxxxx

Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • 13
    Someone pasted this link the other day which I think is apt for this particular situation: http://www.whathaveyoutried.com – Charleh Dec 17 '12 at 15:11
  • 1
    Read the whole thing in as a string, cut off the the part where "if you have.." starts to the end. substring from end of "password is" (which should always be the same starting index) to the end of the new string. – turbo Dec 17 '12 at 15:14
  • is the text a string? `string j = mystring.Remove(0, mystring.LastIndexOf(" "));` Edit, posted this before code outline, remove would still work just not in this way – Sayse Dec 17 '12 at 15:15
  • What if it is a Rich text format string with humankind-alike protection? "Read carefully. Password is at 5-th symbol next after last letter in the word END. So Password10 is quite unexpeted. Your password is :none available. " – Alan Turing Dec 17 '12 at 15:21

4 Answers4

2

Well, if you know for certain that will be the form in which the text will be presented. Always. Then you can simply do something like:

string text = //load your text here;
int startingIndex = text.IndexOf("Your new password is ") + "Your new password is ".Length;
string newText = text.SubString(startingIndex, text.length); //this will load all your text after the password.
//then load the first word
string password = newText.Split(' ')[0];
dutzu
  • 3,883
  • 13
  • 19
0

You might consider using a RegEx (regular expressions) as well.

Miha Markic
  • 3,158
  • 22
  • 28
0

You could use string.Substring:

int indexOfPasswordText = text.IndexOf("Your new password is ");
if (indexOfPasswordText != -1)
{
    int passwordStart = indexOfPasswordText + "Your new password is ".Length;
    int indexeOfNextWord = text.IndexOfAny(new[] { '\n', '\r', ' ' }, passwordStart);
    if (indexeOfNextWord == -1) indexeOfNextWord = text.Length;
    string passWord = text.Substring(passwordStart, indexeOfNextWord - passwordStart);
    Console.Write(passWord);
}

Demo

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

I didn't test this but maybe it can push you in the right direction.

string input = [YOUR MAIL];
string regex = @"Your new password is (\w+)";
Match m = Regex.Match(input, regex);
if (m.Success) {
    string password= m.Groups[1].Value;
    //do something
}
Moriya
  • 7,750
  • 3
  • 35
  • 53