简单的正则表达式 - 解析名字姓氏,性别(男性,女性),年龄或出生年份

时间:2014-07-23 10:56:31

标签: c# regex

我输入了文本文件:

  

Max Smith m 20

     

Dennis Bird m 20

     

John Carter 1970年

     

Elizabeth Cow f 1969

其中

  

名字姓氏性别年龄

  

名字姓氏性别年份出生

我需要解析它并使用 C#和正则表达式分成3个元素:名称,性别,年龄(或YearOfBirth)

1 个答案:

答案 0 :(得分:0)

你只想要"姓名,性别,年龄(或YearOfBirth)" 您仍然可以使用正则表达式,或String.Split

拆分解决方案:

string line = "Max Smith m 20";
string[] words = line.Split(" ");

int nr = items[words.Length - 1]; 
//use number to determine age or year
string gender = items[words.Length - 2];
//string name = combine the leftover elements from the array

对于正则表达式解决方案,您必须考虑从右到左匹配的名称和额外空格:Max D. Smith jr. m 1978(Filburt示例)