Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I want to use Regex to retrieve the person and its address.

The result wild be :
All Frank Anderson and its address inside of a string list.

Problem:
The problem I'm facing is that I cannot retrieve the second name that is "Frank Andre Anderson" based on my regex.

It also might be other people who can have another second name.

Thank you!


C#
string pFirstname = "Frank"
string pLastname = "Anderson";

string input = w.DownloadString("http://www.birthday.no/sok/?f=Frank&l=Anderson");

Match theRegex8 = Regex.Match(input, @"(?<=\>)" + pFirstname + "(.+?)" + pLastname + "</a></h3><p><span>(.+?<)", RegexOptions.IgnoreCase);

foreach (var matchgroup in theRegex8.Groups)
{
    var sss = matchgroup;
}
Posted
Comments
Richard Deeming 2-Jul-15 15:46pm    
Read Falsehoods Programmers Believe About Names[^] - what you're trying to do is almost certainly impossible.
Sergey Alexandrovich Kryukov 2-Jul-15 17:23pm    
5!(Perhaps it would make a good formal answer.)
See also my comment below. It is not as complicated as the article you referenced (a good one!) but it still can dismiss the formulation of the "problem" itself.
—SA
Sergey Alexandrovich Kryukov 2-Jul-15 17:32pm    
Please see the comments above. :-)

I can tell you something: the concept "last name" and "first name" is a big misconception. It is only applicable to some Western cultures, and not for all of them. Interestingly, most of population of the Earth use different official naming patterns, where the "family name" comes before "given name", and some cultures use variable orders. So, no part of the name should be considered "first" or "last". Even the concept of "family name" is not precise, because in some cultures I know (Western!) official name uses something like "family names", but at least two different ones, coming from mother's and father's families. Western formalities (forms, questionnaires) demonstrate spectacular failures here; and not all visitors or immigrants are ready to mangle their official names (especially those using Latin-based writing systems).

Now, about your question: it is not definitive. The example of data is not the definition of the set of all possible data sets or data pattern. You have to define it strictly and formally. The code fragment sometimes could play the role of the definition... only if the code was correct, but then you would not ask your question. Therefore, the question does not make certain sense.

—SA
Kuthuparakkal 5-Jul-15 15:13pm    
Split the string into array (based on spaces), then use LINQ first and last like:
stringArray.First();
stringArray.Last();

1 solution

Please check the below code and let me know....it is working in my system


string pFirstname = "Frank";
string pLastname = "Anderson";
string input = System.IO.File.ReadAllText(@"Data.txt");
var w = new WebClient();
//string input = w.DownloadString("http://www.birthday.no/sok/?f=Frank&l=Anderson");


string pattern = pFirstname + @"(\s)(\w)*(\s)?" + pLastname + @"(\w|\s|\d|,)+";

var regex = new Regex(pattern);

/*string str = Regex.Replace(input, @"(�)|(\<b\>)|(\<\/a\>)|( )|(\<\>)|(\<span\>)|(\<\/span\>)|(\<\/div\>)|(\<\/b\>)|(\<\/h3\>)|(\<\/p\>)|(\<div\>)|(\<h3\>)|(\<p\>)", " ");(\<(\/)?(\w)*(\d)?\>)*/

string str = Regex.Replace(input, @"(�)|(\<(\/)?(\w)*(\d)?\>)", " ");

var match = regex.Match(str);

while (match.Success)
{
Console.WriteLine(str.Substring(match.Index,match.Length));
match = match.NextMatch();
}

Console.ReadLine();
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900