-3

can you help me with regex to find all single quotes inside single quotes?

IE

'sinead o'connor','don't don't','whatever'

Thanks for any advice.

jurkij
  • 155
  • 2
  • 9

3 Answers3

1

Seems like your string was separted by comma.

re.sub(r"\b'\b", "''", s)

or

(?<=[^,])'(?!,|$)

DEMO

Example:

>>> import re
>>> s = "'sinead o'connor','don't don't','whatever'"
>>> re.sub(r"\b'\b", "''", s)
"'sinead o''connor','don''t don''t','whatever'"
>>> 
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • Hi Avinash, thanks for reposnse. Do you know how to build regex which will find all single quotes between single quotes? – jurkij Mar 01 '16 at 09:54
0

You can achieve this even without regex:

>>> string = "'sinead o'connor','don't don't','whatever'"
>>> string = string.replace("'", "''")
"''sinead o''connor'',''don''t don''t'',''whatever''"
>>> string.strip("'")
"sinead o''connor'',''don''t don''t'',''whatever"
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
0

Try this:

'[\w|\s]*([']*)[\w|\s]*'

Check this on regex101.com

sunny
  • 178
  • 3
  • 12