Extracting matches from Regex.Split

Discussion in 'ASP .Net' started by Stephan Bour, Oct 29, 2003.

  1. Stephan Bour

    Stephan Bour Guest

    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;
    }
    }
    }
     
    Stephan Bour, Oct 29, 2003
    #1
    1. Advertising

  2. Stephan Bour

    Stephan Bour Guest

    I figured it out:

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

    in article BBC59931.AF95%, Stephan Bour at
    wrote on 10/29/03 16:20:

    > 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;
    > }
    > }
    > }
    >
     
    Stephan Bour, Oct 29, 2003
    #2
    1. Advertising

  3. Stephan Bour

    mikeb Guest

    Stephan Bour wrote:
    > 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]

    --
    mikeb
     
    mikeb, Oct 29, 2003
    #3
  4. Stephan Bour

    Stephan Bour Guest

    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.

    in article #ZA#, mikeb at
    wrote on 10/29/03 17:35:

    > Stephan Bour wrote:
    >> 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]
    >
    > --
    > mikeb
    >
     
    Stephan Bour, Oct 30, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. darrel
    Replies:
    1
    Views:
    803
    Blair Bonnett
    Jan 3, 2005
  2. Replies:
    4
    Views:
    1,592
  3. Talin
    Replies:
    3
    Views:
    489
    Talin
    Nov 19, 2005
  4. Jeremy
    Replies:
    10
    Views:
    1,347
    Tim Chase
    Jan 13, 2010
  5. Replies:
    6
    Views:
    173
Loading...

Share This Page