Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Replace strings that starts with a char and ends with space

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you could use a regular expression to find the words and put quotes using the re.sub function:

import re

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading