Home > database >  Regex find especific characters between 2 strings
Regex find especific characters between 2 strings

Time:05-06

I have this text:

\r\n\"Ms. Lynn A. Tetrault Esq., J.D.\",Exec. Chairman & Principal Exec. Officer
\r\n\"Ms. Halley E. Gilbert Esq., J.D.\",Chief Legal Officer & Corp. Sec.
\r\nMr. George A. Cardoza,Pres & COO of Lab Operations

I need to match every comma inside the \r\n\" and \", to replace them with nothing, leaving the other commas as they are, like this:

\r\n\"Ms. Lynn A. Tetrault Esq. J.D.\",Exec. Chairman & Principal Exec. Officer
\r\n\"Ms. Halley E. Gilbert Esq. J.D.\",Chief Legal Officer & Corp. Sec.
\r\nMr. George A. Cardoza,Pres & COO of Lab Operations

So I have a regex with positive look ahead and positive look behind:

(?<=\\r\\n\\")(.*?)(?=\\",)

But I don't know how to get only the commas inside to do the replacement, I tried changing the (.*) part but it seems like is all or nothing and I haven't been able to capture only the commas.

Here's an example: Regex101

Here are more samples:

\r\n\"Ms. Lynn A. Tetrault Esq., J.D.\",Exec. Chairman & Principal Exec. Officer
\r\n\"Ms. Halley E. Gilbert Esq., J.D.\",Chief Legal Officer & Corp. Sec.
\r\nMr. George A. Cardoza,Pres & COO of Lab Operations
\r\nMs. Kathryn B. McKenzie,Chief Sustainability & Risk Officer
\r\n\"Prof. Clive D. Morris M.B.A., M.D.\",Pres at Inivata
\r\nMr. William Bishop Bonello,Chief Financial Officer
\r\nMs. Cynthia J. Dieter,Chief Accounting Officer
\r\nMr. John Mooney,Chief Technology Officer
\r\n\"Dr. Shashikant Kulkarni Facmg, M.B.A., M.S., Ph.D.\",Exec. VP of R&D and Chief Scientific Officer
\r\nCharlie Eidson,Director of Investor Relations and Corp. Devel.\r\n",

CodePudding user response:

This works for all your examples:

,(?=.*\\")

It means a comma that has \" appearing somewhere after it.

You'll need some app code or a tool to replace all matches with the blank string.

See live demo.

  • Related