I want to change any None to 'None' (note single quotes) in a JSON file that contains both already.
type: None or
type: 'None'
I tried "s/[']?None[']?/'None'/g" but it doesn't seem to work.
Since you are changing all None to 'None', wouldn't it be easier to just use str.replace with that json string? I'd run it in two procedures, first change 'None' to None, then change all None to 'None'.
You could use a negative lookahead assertion (?!...):
import re
test = "type: None or 'None'"
result = re.sub(r"None(?!')", r"'None'", test)
This will match None as long as it is not directly followed by a '.
'None' to ''None''He Shiming 2012-04-03 23:05
s/([^'])?(None)([^'])?/$1'$2'$3/g