Extracting matches from Regex.Split

  • Thread starter Thread starter Stephan Bour
  • Start date Start date
S

Stephan Bour

I have a string ³Name² in the following format: ³LastName, FirstName
(Department)² that comes from Active Directory. I need to extract the
FirstName from the string. Substrings are not practical for this so I used a
Regex that splits the string at the first ³, ³ and again at the ³ (³. My
problem is that I don¹t know how to extract the second member of the
resulting array (FirstName) and assign it to another string. Using
StringBuilder, as below returns a single string with all the matches
concatenated. Any idea?
Thanks,
Stephan.

public String FirstName {
get {
Regex SplitName = new Regex(" (|, ");
StringBuilder sBuilder = new StringBuilder();
foreach (string Found in SplitName.Split(Name) {
mFirstName = sBuilder.AppendFormat(Found).ToString();
return mFirstName;
}
}
}
 
I figured it out:

public String FirstName {
get {
Regex SplitName = new Regex ("[, ][ (]");
StringBuilder sBuilder = new StringBuilder();
return mFirstName = SplitName.Split(mName)[1];
}
}
 
Stephan said:
I have a string ³Name² in the following format: ³LastName, FirstName
(Department)² that comes from Active Directory. I need to extract the
FirstName from the string. Substrings are not practical for this so I used a
Regex that splits the string at the first ³, ³ and again at the ³ (³. My
problem is that I don¹t know how to extract the second member of the
resulting array (FirstName) and assign it to another string. Using
StringBuilder, as below returns a single string with all the matches
concatenated. Any idea?
Thanks,
Stephan.

public String FirstName {
get {
Regex SplitName = new Regex(" (|, ");
StringBuilder sBuilder = new StringBuilder();
foreach (string Found in SplitName.Split(Name) {
mFirstName = sBuilder.AppendFormat(Found).ToString();
return mFirstName;
}
}
}

It might be good for you to cut-n-paste your sample from code that
you've actually compiled and run. Your sample has a couple of syntax
errors that prevent it from compiling and running. After a certain
number of syntax and logic errors it becomes difficult to know what
you're problem really is and what are simply typos in the posting. For
example, when I fix the syntax errors by guessing at what you might
really be doing, I find the FirstName property returning the last name,
not all the matches concatenated.

In any case, I suspect that what you want is to simply return

SplitName.Split(Name)[1]
 
Point taken. I apologize for the syntax errors and I certainly appreciate
your effort in correcting them in order to help me. Your suggestion is how I
actually ended up doing it (see my own reply to my question for the full -
syntax error free - code).
Stephan.

Stephan said:
I have a string ³Name² in the following format: ³LastName, FirstName
(Department)² that comes from Active Directory. I need to extract the
FirstName from the string. Substrings are not practical for this so I used a
Regex that splits the string at the first ³, ³ and again at the ³ (³. My
problem is that I don¹t know how to extract the second member of the
resulting array (FirstName) and assign it to another string. Using
StringBuilder, as below returns a single string with all the matches
concatenated. Any idea?
Thanks,
Stephan.

public String FirstName {
get {
Regex SplitName = new Regex(" (|, ");
StringBuilder sBuilder = new StringBuilder();
foreach (string Found in SplitName.Split(Name) {
mFirstName = sBuilder.AppendFormat(Found).ToString();
return mFirstName;
}
}
}

It might be good for you to cut-n-paste your sample from code that
you've actually compiled and run. Your sample has a couple of syntax
errors that prevent it from compiling and running. After a certain
number of syntax and logic errors it becomes difficult to know what
you're problem really is and what are simply typos in the posting. For
example, when I fix the syntax errors by guessing at what you might
really be doing, I find the FirstName property returning the last name,
not all the matches concatenated.

In any case, I suspect that what you want is to simply return

SplitName.Split(Name)[1]
 
Back
Top