0

我有一个这样的字符串:

|0|hiddenField|_ EVENTTARGET||0|hidden| _EVENTARGUMENT||0|hiddenField|_ LASTFOCUS||3848|hiddenField|_VIEWSTATE|s+waYnNoWvsZV2X3xG1WKKtdIXQAqyoTV2oVgE/oyaJDgtLOoHSohEYPdTK7NsrM64VCXtyQQ23FOfUzWFNso8FQlu2JSomP7JGAoxpg2RzFfOWjOMaDDo/iKpjDiAjVMUYHdPvKSzTXpsPmqg49y4yEMfViFOowPSoTrojZAIWfAH0DvqvBHOFDozoDfk283k+O5JEXiyJKHXQ6p0IjFZOjr+tIckyRPPH8vJkCo7xntoQ+ZV8KlzrBfJnRthm1XOkkxX73DTW0mByIbATIAdVUwNWQ4lrapOaaxh5y7AjoxlcpyyG2rkezkKquaZyf0kZg5+Yd9HnUmXUY5VsEj2NYIyuppVoYuFQPeoRXbC/SQV6m9Gf++VYhm+sVo8sx8Dvoitelm3R617/zEi71VIrJlk51BH6DnWwaWoHH6gygSHslVwP+iFTao+LR5fekfjAf+BeTgBshc8BVGwslQxJ+YBmyttQxSddb8WyqEGHX2Wc6XqXCKSA8XEgad/42lPRknrLtCLM1b3sn7xWQxUUnb2pEDRc96C+tNUPAy3CZPS2Uq/aCsJqQTp9EssnMhKvfUJTplEbxd9xX2KfeTRa51cDZGzQbBk3L8steG83ehGm1hsU42hdx/3GICA1eKsDmFKpA0D2/NWpnG4rYWJK+MhzbnveqbX0Cak6VyEOLcmaD0dhYz9kOhOhc7h3ntWcbE40qbKhhKTE8Yq9voAqRGFT2AuGTThtbGfQ2GYoua8Oz8pPSgkGYsOcU6dI0vtoEdeH9rUC3a2vLLigVXeQ2bCbAFIkzrpHSfHsp9TLE1AoX3E57//23ZcwzDTJiPYottJGwxn3cnenh8xOdcoQM+7qkDiaD7CVUrvN9p8dmtQjtYHNbt7D8m/SZjvA/SmmAfIKMA==|0|hiddenField|

我需要从这个字符串中获取 __VIEWSTATE 的值:

s+waYnNoWvsZV2X3xG1WKKtdIXQAqyoTV2oVgE/oyaJDgtLOoHSohEYPdTK7NsrM64VCXtyQQ23FOfUzWFNso8FQlu2JSomP7JGAoxpg2RzFfOWjOMaDDo/iKpjDiAjVMUYHdPvKSzTXpsPmqg49y4yEMfViFOowPSoTrojZAIWfAH0DvqvBHOFDozoDfk283k+O5JEXiyJKHXQ6p0IjFZOjr+tIckyRPPH8vJkCo7xntoQ+ZV8KlzrBfJnRthm1XOkkxX73DTW0mByIbATIAdVUwNWQ4lrapOaaxh5y7AjoxlcpyyG2rkezkKquaZyf0kZg5+Yd9HnUmXUY5VsEj2NYIyuppVoYuFQPeoRXbC/SQV6m9Gf++VYhm+sVo8sx8Dvoitelm3R617/zEi71VIrJlk51BH6DnWwaWoHH6gygSHslVwP+iFTao+LR5fekfjAf+BeTgBshc8BVGwslQxJ+YBmyttQxSddb8WyqEGHX2Wc6XqXCKSA8XEgad/42lPRknrLtCLM1b3sn7xWQxUUnb2pEDRc96C+tNUPAy3CZPS2Uq/aCsJqQTp9EssnMhKvfUJTplEbxd9xX2KfeTRa51cDZGzQbBk3L8steG83ehGm1hsU42hdx/3GICA1eKsDmFKpA0D2/NWpnG4rYWJK+MhzbnveqbX0Cak6VyEOLcmaD0dhYz9kOhOhc7h3ntWcbE40qbKhhKTE8Yq9voAqRGFT2AuGTThtbGfQ2GYoua8Oz8pPSgkGYsOcU6dI0vtoEdeH9rUC3a2vLLigVXeQ2bCbAFIkzrpHSfHsp9TLE1AoX3E57//23ZcwzDTJiPYottJGwxn3cnenh8xOdcoQM+7qkDiaD7CVUrvN9p8dmtQjtYHNbt7D8m/SZjvA/SmmAfIKMA==

我已经尝试了几种使用 re 模块的模式,但它们都不起作用。有人可以帮我吗?

4

2 回答 2

2

这有效:

_VIEWSTATE\|([^|]*)

演示:http ://rubular.com/r/JoFyUu5NsC

并加上@dbaupp

(?:^|\|)_VIEWSTATE\|([^|]*)

http://rubular.com/r/HmnapACGEw

于 2012-06-01T10:56:29.587 回答
1

这个正则表达式这样做

_VIEWSTATE\|([^|"]*)

它将视图状态存储在第 1 组中。要使用的 Python 代码是

reobj = re.compile(r"_VIEWSTATE\|([^|"]*)")
match = reobj.search(subject)
if match:
    result = match.group(1)
else:
    result = ""

阅读您的评论,我认为 " 的结束分隔符也是可能的,对吧?您可能正在处理 ASP.NET 视图状态。

于 2012-06-01T11:03:18.410 回答