我想从下面的多行字符串中检索Auth=
值。我试过Python re.match
没有成功。如果我能得到任何帮助,我将不胜感激?
SID = DQAAALsAAABCeyCMlOaYMHkv55TUQFxA71fxE1LpgpmL1G_o8YennFwBhar2I_LNmJjGjvLHVQy8tSRfYdLnUIHhKyD0FTZBzXyG_s8U4Pt97n9hPz68ZFSM42Qv6Qxuk74TQygHJXhjLWXNuD5mMsh8_MAs-nmhSToNFIyWoP-uTZ_LN2yQS1o9MB43fzuIIxp-1euXGxMceVVrjyidrYeEB13HS5kMHH-HGjiZhoIJBmu5es7pLPj9Ie8NJZ1K3kFhdVEJa4sLSID = DQAAAL4AAACypRIVyVXcs5zYIeUEt9v-wEwPKgQ8Oe23_URsDeHCg-rR2qQK4dTxPV1J6BPTO-6Zly2H9t4sVhm0vHe8IT6sKLdX2IQ8PgGMtSHQNkpQ8zEan0CyFyUetbSW4af6mlk2pksDpvXNm5GtNTj5eTwkCQUmgGep42u5iuCGFy-o9a1cQWz45NO_J8zIYnBdOqlheNTqaMWpi4hpr-_u8Muzs4RjlEbkuYfDu7MrdsJAFwxf0BVW2cGBtB-K2jwaK7wAuth = 873hdyjsbcvuei73hckwoxnaodbc8dnskc8HU1mKRqxh6yEU-9tqx148GqC7h90_190ZzxpEZOHAH5HTptliylRXvMPyqPyijMNu21bOA6ZhvZFuL8YNB3KF63YuV0n5TFJd1-rMI2LQIdPMVBnsxnEGrLIeFOugAFCZ_3OelAc4XjeKdDvIowxkNnvaooXT4kxtkQWzieA3JRKy3Y-Lbi7E0qiXC99GtHVDh5VWvdTs2LCv3wnRULtLp6ZCoToZ8qoUWMzU9PZldsKSnE
答案 0 :(得分:3)
import re
text = "SID=DQAAALsAAABCeyCMlOaYMHkv55TUQFxA71fxE1LpgpmL1G_o8YennFwBhar2I_LNmJjGjvLHVQy8tSRfYdLnUIHhKyD0FTZBzXyG_s8U4Pt97n9hPz68ZFSM42Qv6Qxuk74TQygHJXhjLWXNuD5mMsh8_MAs-nmhSToNFIyWoP-uTZ_LN2yQS1o9MB43fzuIIxp-1euXGxMceVVrjyidrYeEB13HS5kMHH-HGjiZhoIJBmu5es7pLPj9Ie8NJZ1K3kFhdVEJa4sLSID=DQAAAL4AAACypRIVyVXcs5zYIeUEt9v-wEwPKgQ8Oe23_URsDeHCg-rR2qQK4dTxPV1J6BPTO-6Zly2H9t4sVhm0vHe8IT6sKLdX2IQ8PgGMtSHQNkpQ8zEan0CyFyUetbSW4af6mlk2pksDpvXNm5GtNTj5eTwkCQUmgGep42u5iuCGFy-o9a1cQWz45NO_J8zIYnBdOqlheNTqaMWpi4hpr-_u8Muzs4RjlEbkuYfDu7MrdsJAFwxf0BVW2cGBtB-K2jwaK7w*Auth=*873hdyjsbcvuei73hckwoxnaodbc8dnskc8HU1mKRqxh6yEU-9tqx148GqC7h90_190ZzxpEZOHAH5HTptliylRXvMPyqPyijMNu21bOA6ZhvZFuL8YNB3KF63YuV0n5TFJd1-rMI2LQIdPMVBnsxnEGrLIeFOugAFCZ_3OelAc4XjeKdDvIowxkNnvaooXT4kxtkQWzieA3JRKy3Y-Lbi7E0qiXC99GtHVDh5VWvdTs2LCv3wnRULtLp6ZCoToZ8qoUWMzU9PZldsKSnE"
m = re.search('Auth=(.+)',text).groups()[0]
print m
结果: '* 873hdyjsbcvuei73hckwoxnaodbc8dnskc8HU1mKRqxh6yEU-9tqx148GqC7h90_190ZzxpEZOHAH5HTptliylRXvMPyqPyijMNu21bOA6ZhvZFuL8YNB3KF63YuV0n5TFJd1-rMI2LQIdPMVBnsxnEGrLIeFOugAFCZ_3OelAc4XjeKdDvIowxkNnvaooXT4kxtkQWzieA3JRKy3Y-Lbi7E0qiXC99GtHVDh5VWvdTs2LCv3wnRULtLp6ZCoToZ8qoUWMzU9PZldsKSnE'
答案 1 :(得分:2)
尝试使用re.search
代替re.match
,match checks for a match only at the beginning of the string, while search checks for a match anywhere in the string。
在创建正则表达式时,您可能还需要re.MULTILINE
标志,这样的事情可以满足您的需求:
re.search("Auth=(.*)", data, re.MULTILINE)
如果验证不是最后一个值,请将.*
替换为匹配的正则表达式,直到下一个值。
答案 2 :(得分:1)
安德鲁的回答是正确的。另外,我会提到命名组功能。您可以轻松地从正则表达式中提取部件:
import re
text = "SID=whateverAuth=myauthvalue"
m = re.search('Auth=(?P<auth>.+)', text, re.MULTILINE)
print m.group('auth') #should print myauthvalue
Python官方文档: http://docs.python.org/library/re.html
答案 3 :(得分:0)
不需要regexp,而是在行上循环并在=
上拆分。或许有点啰嗦......
#!/usr/bin/python
with open("auth") as fd:
for line in fd:
if 'Auth' in line:
auth=line.split('=')[-1].strip()
found=True
if found:
auth.join(line.strip())
*873hdyjsbcvuei73hckwoxnaodbc8dnskc8HU1mKRqxh6yEU-9tqx148GqC7h90_190ZzxpEZOHAH5HTptliylRXvMPyqPyijMNu21bOA6ZhvZFuL8YNB3KF63YuV0n5TFJd1-rMI2LQIdPMVBnsxnEGrLIeFOugAFCZ_3OelAc4XjeKdDvIowxkNnvaooXT4kxtkQWzieA3JRKy3Y-Lbi7E0qiXC99GtHVDh5VWvdTs2LCv3wnRULtLp6ZCoToZ8qoUWMzU9PZldsKSnE
答案 4 :(得分:0)
如果字符串是多行并且总是以相同的顺序(SID,LSID,Auth),则可以使用:
wantedresult = originalstring.strip().split('\n')[-1].split('=')[-1]
strip
是必要的,以删除大多数服务器包含在原始字符串末尾的额外换行符。