Skip to main content
 首页 » 编程设计

c#之如何在 C# 正则表达式中将名称与斜线匹配

2024年02月05日3152php

我有一个很长的文本,其中包含如下字符串:

... 
1.1SMITH/JOHN 2.1SMITH/SARA 
... 
1.1Parker/Sara/Amanda.CH07/Elizabeth.IN03 
... 

C# 中是否有任何正则表达式可以匹配这些名称。线索是搜索以'/'分隔的[A-Z]。

请您参考如下方法:

你可以试试这个:

[a-zA-Z\/]+ 

Explanation

C# 示例:

using System; 
using System.Text.RegularExpressions; 
 
public class Example 
{ 
    public static void Main() 
    { 
        string pattern = @"[a-zA-Z\/]+"; 
        string input = @"... 
1.1SMITH/JOHN 2.1SMITH/SARA 
... 
1.1Parker/Sara/Amanda.CH07/Elizabeth.IN03"; 
 
        foreach (Match m in Regex.Matches(input, pattern)) 
        { 
            Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index); 
        } 
    } 
} 

You can test the working c# sample here