# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"set \$(\w+)\s+\"?(.+?)\"?$"
test_str = ("#========= Variables =========\n"
"set $win Mod4\n"
"set $alt Mod1\n"
"set $ws1 \"1\"\n"
"set $ws2 \"2\"\n"
"set $ws3 \"3\"\n"
"set $ws4 \"4\"\n"
"set $ws5 \"5\"\n"
"set $ws6 \"6\"\n"
"set $ws7 \"7\"\n"
"set $ws8 \"8\"\n"
"set $ws9 \"9\"\n"
"set $ws0 \"0\"\n"
"set $bar $HOME/.config/polybar/launch.sh\n"
"set $locker i3lock -i $HOME/.config/i3/bg.png\n"
"set $term kitty\n"
"set $menu dmenu -i -l 10\n"
"set $browser qutebrowser\n"
"set $filemanager kitty -e ranger\n"
"set $launcher dmenu_run\n"
"set $notify_daemon dunst\n"
"set $notify_bar_program xob\n"
"set $notify_bar /tmp/xobpipe\n"
"set $script $HOME/.config/i3/scripts\n"
"set $last_action /tmp/i3/last_action\n"
"set $counter /tmp/i3/counter\n"
"set $windows /tmp/i3/windows\n"
"set $current_workspace i3-msg -t get_workspaces | jq -r '.[] | select(.focused==true).name'\n"
"#============================\n\n"
"#======== Autostart =========\n"
"exec nm-applet\n"
"exec kotatogram-desktop\n"
"exec $browser\n"
"exec --no-startup-id dunst\n"
"#============================")
subst = "\\1=\"\\2\""
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html