I am trying to parse through a string of known format to obtain variables for speed and direction (basically recreating sscanf functionality), an example string shown below
s = 'speed: 100.0, direction[ 12 ]'
However, the square brackets after direction are causing me problems. I have tried
checker=re.search('speed: (?P<speed>[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?), direction\[ (?P<direc>\d) \]',s)
print(f"[{checker['speed']},{checker['direc']}]")
adding \ before the square brackets as suggested here: https://stackoverflow.com/a/74477176/4879524
However, this is not working, and I am unsure how to proceed. If I remove the square brackets from the string it works fine, but I wish to avoid doing that if possible.
My regex knowledge is about 4 hours old so it may be a very simple fix. I cannot use parse module as an alternative sadly
WITH SQUARE BRACKETS - There is no match so...
TypeError: 'NoneType' object is not subscriptable
WITHOUT SQUARE BRACKETS
s = 'speed: 100.0, direction 12'
checker = re.search('speed: (?P<speed>[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?), direction (?P<direc>\d)',s)
print(f"[{checker['speed']},{checker['direc']}]")
>>[100.0,1] # (yes I forgot the + when I wrote it out in stack so here's the answer without the +, you can see that's not causing my error)