menteith
menteith

Reputation: 678

Regex to match a few possible strings with possible leading and/or trailing spaces

Let's say I have a string:

John Smith (auth.), Mary Smith, Richard Smith (eds.), Richie Jack (ed.), Jack Johnny (eds.)

I would like to match:

John Smith(auth.),Mary Smith,Richard Smith(eds.),Richie Jack(ed.),Jack Johnny(eds.)

I have came up with a regex but I have a problem with the | (or character) because my string contains characters that have to be escaped like ().. This is what I'm not able deal with. My regex is:

\s+\((auth\.\)|\(eds\.\))?,\s+

EDIT: I think now that the most universal solution would be to assume that in () could be anything.

Upvotes: 2

Views: 186

Answers (1)

Randy Flamini
Randy Flamini

Reputation: 61

Try this:

\s*\((auth|eds?)?\.\)?,?\s*

\s+ means one or more \s* means zero or more

Based on your comment, I modified the regex:

\s*((\([^)]*\))|,)\s*

Upvotes: 2

Related Questions