0

in my opinion it is a common scenario

the string is something like :

"this is my story, if it's interesting [email protected] so thanks for your time"

i need to make it somthing like

"this is my story, if it's interesting  so thanks for your time"
"[email protected]"

my code for now is trying to count Down from the index of the "@" so it is less times to check within the iteration of the for loop

        public string formatResultContentAndEmail(string source)
        {
            char[] Str2CahrArr = source.ToCharArray();
            var trgt = source.IndexOf('@');
            var stepsBack=0;
            for (int i = trgt; i >0; i--)
            {
                var test = Str2CahrArr[i];
                if (Str2CahrArr[i].Equals(" "))
                {
                    stepsBack = i; break;
                }
            }

            return "";//<======change this when done tests
        }

my first problem in that try was i couldn't find when it hits a space .

but even when i'll solve that problem, is that approach is the right one ?

what is the simplest way to extract the mail substring of that complete paragraph ?

3 Answers 3

3

Perhaps there is a better regex approach which searches real emails, this is readable and efficient:

string text = "this is my story, if it's interesting [email protected] so thanks for your time";
if(text.Contains('@'))
{
    string[] words = text.Split();
    string[] emails = words.Where(word => word.Contains('@')).ToArray();
    text = string.Join(" ", words.Where(word => !word.Contains('@')));
}

Demo

this is my story, if it's interesting so thanks for your time
[email protected]
Sign up to request clarification or add additional context in comments.

2 Comments

yep that sure looks more human, than others regex, and i guess in my case is the best to use . thanks for thinking simple.. if it is (:
and if i could I'd give you another +1 for that new(at least for me) online ide benchmarker / test environment
1
public string[] ExtractEmails(string str)
{
    string RegexPattern = @"\b[A-Z0-9._-]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6}\b";

    // Find matches
    System.Text.RegularExpressions.MatchCollection matches = System.Text.RegularExpressions.Regex.Matches(str, RegexPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);

    string[] MatchList = new string[matches.Count];

    // add each match
    foreach (System.Text.RegularExpressions.Match match in matches)
        MatchList[c] = match.ToString();

    return MatchList;
}

Source: http://coderbuddy.wordpress.com/2009/10/31/coder-buddyc-code-to-extract-email/

If you need a better regular expression pattern, you can probably find one at http://www.regular-expressions.info/

1 Comment

i guess i'll get more comfertable with Regex in the futere, but for now i will firstly test @Tim approach . Thanks, i sure will look in to that later on though!
0

My suggestion would be as follows, using String.split()

public String getMail(String inp) {
  String[] prts = inp.split(" ");
  for(String tmp : prts) {
    if(tmp.contains("@")) {
      return tmp;
    }
  }
}

This will break on strings with more than one email, but the fix for that should be trivial.

Comments

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.