473,386 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Extracting matches from Regex.Split

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

Nov 17 '05 #1
3 1717
I figured it out:

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

in article BB*****************@niaid.nih.gov, Stephan Bour at
sb***@niaid.nih.gov 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;
}
}
}


Nov 17 '05 #2
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

Nov 17 '05 #3
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 #Z**************@TK2MSFTNGP11.phx.gbl, mikeb at
ma************@mailnull.com 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


Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Lui Ali | last post by:
Hello, Problem: I want to get the whole text between the opening tag <%OPTIONAL%> and the closing tag <%/OPTIONAL%>. The right block is found by (and contains) the placeholder...
5
by: Markus Ernst | last post by:
Hello I have a regex problem, spent about 7 hours on this now, but I don't find the answer in the manual and googling, though I think this must have been discussed before. I try to simply...
5
by: Michael Hill | last post by:
Hi, folks. I am writing a Javascript program that accepts (x, y) data pairs from a text box and then analyzes that data in various ways. This is my first time using text area boxes; in the past,...
0
by: Mico | last post by:
I would be very grateful for any help with the following: I currently have the code below. This opens a MS Word document, and uses C#'s internal regular expressions library to find if there is a...
5
by: Jensen bredal | last post by:
Hello, I have a string formated in the following way: s = 1;32;100;32;09;.........;09;76; I need to extract the numbers separated by the seicolon. The list can contain several thousands of...
0
by: Bubbles | last post by:
Hello. New to ASP.NET and struggling on this one. I have a text file with a bunch of text in it. Throughout the file words followed by a ":" will appear. I need to pull every such string out...
1
by: John Seeliger | last post by:
I am pretty new to VB, so please forgive the simplistic question. This is using VB .NET Standard 2003. My form has three objects on it: a TextBox named URL, a Button named Extract and a...
3
by: | last post by:
I'm analyzing large strings and finding matches using the Regex class. I want to find the context those matches are found in and to display excerpts of that context, just as a search engine might....
2
by: daveftl | last post by:
Hello, i've tried to extract certain data using Regex in a File. but it seems not working.No errors and warnings have been found. here is my code: Private Sub extractTxt(ByVal inputFile As...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.