I have trouble putting quotes to strings with the char @ and ends with the space, for example:
mystring = "@id string,@type string,account struct<@id string,@type string,accountId:string>"
mystring_expected = "`@id` string,`@type` string,account struct<`@id` string,`@type` string,accountId:string>"
How can I do it in Python?
CodePudding user response:
Just use a regex to match @
and one or more word characters, then wrap that with `
import re
mystring = "@id string,@type string,account struct<@id string,@type string,accountId:string>"
new_mystring = re.sub(r"(@\w )", r"`\1`", mystring)
mystring_expected = "`@id` string,`@type` string,account struct<`@id` string,`@type` string,accountId:string>"
print(new_mystring == mystring_expected)
CodePudding user response:
you could use a regular expression to find the words and put quotes using the re.sub
function:
mystring = "@id string,@type string,account struct<@id string,@type string,accountId:string>"
pattern = r"(@\w \b)"
mystring_expected = re.sub(pattern, r"`\1`", mystring)
print(mystring_expected)