RegEx expert advice needed

Hi ReEx experts!

I am trying to match the name and nickname of a person whose records are provided in this format:

Lastname, Firstname (nickname) (ID)

Ex: Doe, Robert (Bob) (1234)

Nickname is optional so it could be there, or not.
I want to extract just the part of the name that precedes the ID, including the nickname if it’s there. Ex: Doe, Robert (Bob) . A name without a nickname should also be captured.

This is the closest I could get to it, but I’m still missing the trailing parenthesis for the nickname:

[a-zA-Z0-9,().!? ]*[^\\(0-9\\)]

Any idea about what I need to add to capture the full name/nickname? Thanks!

I don’t have a solution but already see an issue with [a-zA-Z...
That will not match names with diacritics such as , neither such as O’Connor

If you absolutely need RegEx then wait for an expert to come in, @KemTekinay maybe?

I would use a different approach by splitting the string using “(” character and then joining without the last n-th field.

If I understand, the two possible forms before the id that you want to capture are:

Doe, Robert
Doe, Robert (Bob)

Is that it?

[quote=489632:@Kem Tekinay]If I understand, the two possible forms before the id that you want to capture are:

Doe, Robert
Doe, Robert (Bob)

Is that it?[/quote]

Exactly!

I have a pattern in mind but won’t be able to test it for about an hour or two. I’ll post then.

Try this:

\\b[^,\\s]+, [^(\\s]+( \\([^)]+\\))?(?= \\(\\d+\\))

[quote=489638:@Kem Tekinay]Try this:

\\b[^,\\s]+, [^(\\s]+( \\([^)]+\\))?(?= \\(\\d+\\)) [/quote]

Thank you Kem!!