Ok, I have a tricky problem here. Yes, I have a code that does something like this, but it is limited, and this one seem to be closer to a more reasonable code. This one doesn’t factor into whether ‘(’ or ‘{’ are next to types, and that makes it a improvement. In addition, it can find variable name and types outside of cases where they’re not next to #@gui.
Behold this code:
import re
sample_str="""#@gui Sample Code: fx_rep_sample
#@gui :Integer Value=int(0,0,5)
#@gui :Float value=float(0,0,5)
#@gui :Choices=choice(0,"First Choice","Second Choice")
#@gui :Point Location=point(50,50)
#@gui :Press this Button=button()
#@gui :Text Input=text("Here's a text")
#@gui :Checkmark=bool(0)
#@gui :Color A=color(0,50,20)
#@gui :Color B=color(210,55,180,220)
#@gui :_=note("Separated"),Variable After Comma=choice(0,"A","B")"""
result_re=re.findall(r'(\#\@gui\ :)(.*,)?(.*)=(int|choice|text|bool|color|point|button|float)',sample_str)
print(result_re[0][3])
The goal is to turn sample_str into this (and the reverse):
#@gui Sample Code: fx_rep_sample
#@gui :1.Integer Value=int(0,0,5)
#@gui :2.Float value=float(0,0,5)
#@gui :3.Choices=choice(0,"First Choice","Second Choice")
#@gui :4.5.Point Location=point(50,50)
#@gui :6.Press this Button=button()
#@gui :7.Text Input=text("Here's a text")
#@gui :8.Checkmark=bool(0)
#@gui :9.10.11.Color A=color(0,50,20)
#@gui :12.13.14.15.Color B=color(210,55,180,220)
#@gui :_=note("Separated"),16.Variable After Comma=choice(0,"A","B")
Here are some of the rules:
int => 1
float => 1
choice => 1
point => 2
button => 1
text => 1
bool => 1
color with 3 number => 3
color with 4 number => 4
Lines in which regex doesn’t find anything should not be modified.
Some note I made is that splitlines can be used to solve most problem except the last line. That’s the part I’m stumped on. Do I use regex split to solve that?