Extracting matches from Regex.Split

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;
}
}
}
 
S

Stephan Bour

I figured it out:

public String FirstName {
get {
Regex SplitName = new Regex ("[, ][ (]");
StringBuilder sBuilder = new StringBuilder();
return mFirstName = SplitName.Split(mName)[1];
}
}
 
M

mikeb

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]
 
S

Stephan Bour

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]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top