C# 简单正则表达式-解析名字、姓氏、性别(男性、女性)、年龄或出生年份
我已输入文本文件: 马克斯·史密斯20岁 丹尼斯·伯德20岁 约翰·卡特1970年出生 伊丽莎白·考夫1969 在哪里 姓姓性别年龄 或 名姓性别出生年份 我需要解析它并使用正则表达式将其拆分为3个元素:姓名、性别、年龄(或出生年份)您只需要“姓名、性别、年龄(或出生年份)” 您仍然可以使用正则表达式,或者: 拆分解决方案:C# 简单正则表达式-解析名字、姓氏、性别(男性、女性)、年龄或出生年份,c#,regex,C#,Regex,我已输入文本文件: 马克斯·史密斯20岁 丹尼斯·伯德20岁 约翰·卡特1970年出生 伊丽莎白·考夫1969 在哪里 姓姓性别年龄 或 名姓性别出生年份 我需要解析它并使用正则表达式将其拆分为3个元素:姓名、性别、年龄(或出生年份)您只需要“姓名、性别、年龄(或出生年份)” 您仍然可以使用正则表达式,或者: 拆分解决方案: string line = "Max Smith m 20"; string[] words = line.Split(" "); int nr = items[word
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举例)您考虑过String.Split
?这里不需要正则表达式。您的要求不是问题。你试过什么?是的,我试过了。但是我是正则表达式的新手,我无法解析“John Carter m 1970”。我可以向您介绍一下Max D.Smith jr.m 1978
?@Filburt:的确,对于string.split,这将很难(再次剪切和附加元素)。那么Regex就更合适了。