1

I am trying to figure out a regular expression for extracting just the first name from Last, First Middle

For example: Smith, Jane Ann returns just Jane.

I figured out the last name field by using ^[a-zA-Z_'^\s]+.

I believe I want the first name extracted by grabbing the first word after the comma, but I'm at a loss as to how to do that. Also, these are two separate fields, so the first name needs to be singled out.

[^,]*$ gives me both first and middle names

/(?<=, ).*(?=) also gives me both first and middle names

Anton Menshov
  • 2,266
  • 14
  • 34
  • 55
  • 2
    The only correct option is to let the user enter what they say their first name is. For example, from my username here on SE, "Andrew Morton", you would probably not manage to extract "Drew" as my "first" name. – Andrew Morton Dec 09 '22 at 22:04

1 Answers1

1

Use the lookbehind to match the comma, and follow that with a more specific pattern that matches non-whitespace characters, so it won't include the middle name.

(?<=, )\S+
Barmar
  • 741,623
  • 53
  • 500
  • 612