Sepinood Sepinood - 1 month ago 7
C# Question

How to match names with slash in C# regex?

I have a long text which contains strings like these:

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


Is there any regular expression in C# which can match these names. The clue is to search for [A-Z] which has separated by '/'.

Answer

You can try this:

[a-zA-Z\/]+

Explanation

c# sample:

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

Comments