0

I am using C# and in one of the places i got list of all peoples names with their email id's in the format

name(email)\n

i just came with this sub string stuff just off my head. I am looking for more elegant, fast ( in the terms of access time, operations it performs), easy to remember line of code to do this.

string pattern =  "jackal(jackal@gmail.com)";
string email = pattern.SubString(pattern.indexOf("("),pattern.LastIndexOf(")") - pattern.indexOf("("));

//extra
string email =  pattern.Split('(',')')[1];

I think doing the above would do sequential access to each character until it finds the index of the character. Works ok now since name is short, but would struggle when having a large name ( hope people don't have one)

Deeptechtons
  • 10,945
  • 27
  • 96
  • 178
  • 9
    I can totally see this struggling when handling names over several million characters long. – Jon Jul 09 '12 at 09:15
  • looks like a job for a simple regex. – leppie Jul 09 '12 at 09:16
  • 1
    Use lastIndexOf (there should be one - I think) to find closing parentheses `)`. – nhahtdh Jul 09 '12 at 09:16
  • Sure it's fine, although leppie's regex suggestion is probably a better practical decision. – Jon Jul 09 '12 at 09:16
  • 3
    @Jon Practical only if it can be understood in a few months time lol – Adam Houldsworth Jul 09 '12 at 09:17
  • "But it's too simple. It possibly couldn't work". – Asti Jul 09 '12 at 09:18
  • @AdamHouldsworth: anyone spending more than an hour understanding a regex, should probably not be using it. It is no harder than any programming language. – leppie Jul 09 '12 at 09:19
  • @nhahtdh i suppose `)` is not allowed in emails do they? we also do validate the emails in first place before they get inside the storage.hence `firstIndex` seemed perfect for this – Deeptechtons Jul 09 '12 at 09:20
  • 2
    @leppie It isn't the person using it that I'm worried about, it's the person coming along to decipher it and any intricacies *afterwards*. It is no harder, granted, but it certainly isn't crystal clear. – Adam Houldsworth Jul 09 '12 at 09:20
  • can people have a `"("` in thier name? – Jodrell Jul 09 '12 at 09:23
  • @Deeptechtons: I thought the data has been cleaned. If the data has not been validated, then regex can be used to validate + extract data at the same time. `)` is actually allowed in email address but with restriction - not sure if anyone actually has one, though. – nhahtdh Jul 09 '12 at 09:25
  • @Jon what do you think of extra section ? split method would work ? but seems it will always create a array of 3 elements ( not much space, but i should rethink) – Deeptechtons Jul 09 '12 at 09:30
  • @Jodrell NOPE :) we don't allow doing that – Deeptechtons Jul 09 '12 at 09:31

2 Answers2

0

A dirty hack would be to let microsoft do it for you.

try
{
    new MailAddress(input);
    //valid
}
catch (Exception ex)
{
    // invalid
}

I hope they would do a better job than a custom reg-ex.

Maintaining a custom reg-ex that takes care of everything might involve some effort.

Refer: MailAddress

Your format is actually very close to some supported formats. Text within () are treated as comments, but if you replace ( with < and ) with > and get a supported format.

nunespascal
  • 17,584
  • 2
  • 43
  • 46
0

The second parameter in Substring() is the length of the string to take, not the ending index.

Your code should read:

string pattern = "jackal(jackal@gmail.com)";

int start = pattern.IndexOf("(") + 1;
int end = pattern.LastIndexOf(")");
string email = pattern.Substring(start, end - start);

Alternatively, have a look at Regular Expression to find a string included between two characters while EXCLUDING the delimiters

Community
  • 1
  • 1
magritte
  • 7,396
  • 10
  • 59
  • 79