Payam
Payam

Reputation: 751

Using regular expression to extract values that contain comma

I am working with a text such as this:

Graham, Mckenna -- ut    Voluptatem ipsam et at.Marvin, Garfield -- non    Facere et necessitatibus animi. McLaughlin, Mariah -- consequatur Eveniet temporibus ducimus amet eaque. Lang, Agustina -- pariatur

As you can see, valid English names are separated by comma and a space. I am looking for a regular expression pattern to extract such substring. Comma should be included in the substring but not the trailing white space. I haven't had much luck in finding the correct pattern yet.

my code in C# looks like this:

var value = reader.ReadLine();
var regex = new Regex(@"[A-Z]\w+,(?=\s)");
var match = regex.Match(value);

Upvotes: 1

Views: 65

Answers (2)

hwnd
hwnd

Reputation: 70722

If you are trying to match the names before the comma and after the comma, you can use the following.

var rgx = new Regex(@"[A-Z][a-zA-Z]+, *[A-Z][a-zA-Z]+");

Or you could simplify the pattern to the following in this case:

var rgx = new Regex(@"(?i)[a-z]+, *[a-z]+");

Ideone Demo

Upvotes: 2

Bohemian
Bohemian

Reputation: 424983

If there's always exactly 2 name parts:

/[a-z]+, [a-z]+/i

See demo using your input.

Upvotes: 0

Related Questions