I'm very, very new to regular expressions. I just picked it up about 3 hours ago, but I hit a snag, and I can't seem to shake it. So, as always, I turned to the internet to solve all my problems, and when it couldn't explain the answers to me, I searched on stackoverflow to see if someone else had asked my question, and finally just posted a new question when I couldn't answer it myself from browsing.
I'll dumb down what I'm trying to do a little bit because I've figured most of it out, but this one teeny weeny bit of it just isn't working the way I want it to, or at all actually, and the whole mess is complicated and hard to explain, but in the end, I have a whole bunch of strings I want to run a regex on.
So, in side a repeating loop, I pass a string which contains a variable name. Now, I'm having a hard time explaining what the variables may look like, so I am just going to list of examples, followed by a pipe, followed that what I want to extract.
Variable | (Variable)
Variable.list[3]name | (Variable.list[3]name)
Var.list[5] | (Var.list , 5)
Var.list_name[3]thing_words[4][3][2] | (Var.list_name[3]thing_words , 4 , 3 , 2)
Var[3] | (Var , 3)
Var.word | (Var.word)
And so on. I think that makes it clear, right? I want the variable name, which may or may not contain brackets, and if there are any trailing brackets, I want to exclude them from the name and capture them so I can access them from match.groups(). I don't think there are any variable with a name that ends with ...[] without a number inside, but there may be, and if there are, I want to ignore those too.
Right now I am trying to do something like:
for line in list:
regex = re.compile("^[-\w\[\]\.]+(\[(0-9)*]\])*$")
match = regex.match(line)
if match:
do something that depends on len( match.groups() )
But... it's not working. The regex never matches, even when I think it should.
In my mind, I am being very clear! I want it to start with a bunch of stuff and potentially end with a bunch of bracketted numbers, and if it ends with bracketed numbers, to catch them and store them, but ignore any bracketted numbers that are NOT at the end of the string.
So... now that I have thoroughly over explained myself to the point of being a little redundant... what do I do to make it work as I want? Can it even be done the way I am trying to do it? Should I instead do something more like:
while (match.endswith("]")
match.strip("]")
func()
match.strip("[")
where the func() does a regex to strip the number off the end? That seems overly complicated, and very messy. My gut tells me regex can handle it, and that my novice eyes just can't see how.