Chuck Esterbrook <echuck@...> wrote:
> How do you parse something like:
> $(foo bar=")")
> with a regex? My impression is that you can't.
Not really -- I guess that's why <input type=submit
value="next>>"> is very convienient for parsers, if no one else.
Anyway, you can do something like:
def getExpr(s, pos):
dollarPos = string.find(s, "$", pos)
if s[dollarPos+1] == "(":
return getKeywordExpr(s, pos)
return getNamedValue(s, pos)
def getKeywordExpr(s, pos):
nv, keywordPos = getNamedValue(s, pos)
keyword, equalPos = getKeyword(s, keywordPos)
if not keyword:
if s[equalPos] == ")":
raise ParseError, ") expected"
<okay, I'm not going to try to do the entire thing here. You
strip an equal, get a quoted/nonquoted keyword, and continue
until there is an equal where the keyword would be... in other
words, a really little parser -- which actually doesn't need
a regex at all>