C# 用于选择多个名称并避免单个名称的正则表达式
我有个案子。我想从输入中选择多个乘客姓名。在这种情况下,条件是当输入仅包含单个乘客姓名时,请避免使用此输入字符串 我为这种情况创建正则表达式。从输入中选择多个姓名是可行的,但如果我想在输入中避免使用单个乘客姓名,则不可行 我的目标是,我只想选择那些包含多个乘客姓名的案例,而不是单个乘客姓名C# 用于选择多个名称并避免单个名称的正则表达式,c#,regex,C#,Regex,我有个案子。我想从输入中选择多个乘客姓名。在这种情况下,条件是当输入仅包含单个乘客姓名时,请避免使用此输入字符串 我为这种情况创建正则表达式。从输入中选择多个姓名是可行的,但如果我想在输入中避免使用单个乘客姓名,则不可行 我的目标是,我只想选择那些包含多个乘客姓名的案例,而不是单个乘客姓名 Regex regex = new Regex(@"(\d+\.[a-zA-Z]\S(.+))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
Regex regex = new Regex(@"(\d+\.[a-zA-Z]\S(.+))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
foreach (Match m in regex.Matches(item))
{
name = m.ToString();
}
仅供参考,我的正则表达式可能不是最优化的,仍在学习中 根据“示例”,即:
1.ALVARADO/RITA(ADT) 2.CABELLO/LUIS CARLOS STEVE(ADT)
为了至少提取一个名称,我使用了以下正则表达式:
Regex regex = new Regex(@"(\d+\.\w+/\w+(( \w+)+)?\(\w+\))");
Regex regex = new Regex(@"(\d+\.\w+/\w+ \w+(( \w+)+)?\(\w+\))");
为了提取多个名称(两个或更多),我使用了以下正则表达式:
Regex regex = new Regex(@"(\d+\.\w+/\w+(( \w+)+)?\(\w+\))");
Regex regex = new Regex(@"(\d+\.\w+/\w+ \w+(( \w+)+)?\(\w+\))");
然后,为了检索名字和姓氏,我进行了一些字符串操作:
// Example string
string item = @"1.ALVARADO/RITA(ADT) 2.CABELLO/LUIS CARLOS STEVE(ADT)";
// Create a StringBuilder for output
StringBuilder sb = new StringBuilder();
// Create a List for holding names (first and last)
List<string> people = new List<string>();
// Regex expression for matching at least two people
Regex regex = new Regex(@"(\d+\.\w+/\w+ \w+(( \w+)+)?\(\w+\))");
// Iterate through matches
foreach(Match m in regex.Matches(item)) {
//Store the match
string match = m.ToString();
// Remove the number bullet
match = match.Substring(2);
// Store location of slash, used for splitting last name and rest of string
int slashLocation = match.IndexOf('/');
// Retrieve the last name
string lastName = match.Substring(0, slashLocation);
// Retrieve all first names
List<string> firstNames = match.Substring(slashLocation + 1, match.IndexOf('(') - slashLocation -1).Split(' ').ToList();
// Push first names to List of people
firstNames.ForEach(a => people.Add(a + " " + lastName));
}
// Push list of people into a StringBuilder for output
people.ForEach(a => sb.AppendLine(a));
// Display people in a MessageBox
MessageBox.Show(sb.ToString());
//示例字符串
字符串项=@“1.ALVARADO/RITA(ADT)2.CABELLO/LUIS CARLOS STEVE(ADT)”;
//为输出创建StringBuilder
StringBuilder sb=新的StringBuilder();
//创建保存姓名的列表(第一个和最后一个)
列表人员=新列表();
//用于匹配至少两个人的正则表达式
正则表达式正则表达式=新正则表达式(@“(\d+\.\w+/\w+\w+(\w+)?\(\w+\)”);
//迭代匹配
foreach(正则表达式中的匹配m.Matches(项)){
//储存火柴
字符串匹配=m.ToString();
//删除数字项目符号
匹配=匹配。子字符串(2);
//斜杠的存储位置,用于拆分姓氏和字符串的其余部分
int slashLocation=match.IndexOf('/');
//检索姓氏
string lastName=match.Substring(0,slashLocation);
//检索所有名字
List firstNames=match.Substring(slashLocation+1,match.IndexOf(“(”)-slashLocation-1).Split(“”).ToList();
//将名字推送到人员列表中
firstNames.ForEach(a=>people.Add(a+“”+lastName));
}
//将人员列表推送到StringBuilder中进行输出
people.ForEach(a=>sb.AppendLine(a));
//在消息框中显示人物
Show(sb.ToString());
使用此正则表达式,它将帮助您
(2.[A-z]\S(+)你能给我们看一些你输入的例子吗?我猜它是
1.乘客一号,一些信息,2.Pessange二号,一些其他信息,
。是吗?@Maloric..我正在粘贴一个链接。它包含两个输入字符串。一个字符串包含多个名称,另一个仅包含一个名称。@BilalMirza..请查看Pastebin.com链接。我粘贴了我的评论。