Use Python Re module to extract text from multiline
I'd like to retrieve the Auth=
value from the multiline string below. I've tried Python re.match
with no success. Will appreciate if I can get any help?
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-Lbi7E0qiXC99GtHVDh5VWvdTs2LCv3wnRULtLp6ZCoToZ开发者_开发技巧8qoUWMzU9PZldsKSnE
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
Results in: '*873hdyjsbcvuei73hckwoxnaodbc8dnskc8HU1mKRqxh6yEU-9tqx148GqC7h90_190ZzxpEZOHAH5HTptliylRXvMPyqPyijMNu21bOA6ZhvZFuL8YNB3KF63YuV0n5TFJd1-rMI2LQIdPMVBnsxnEGrLIeFOugAFCZ_3OelAc4XjeKdDvIowxkNnvaooXT4kxtkQWzieA3JRKy3Y-Lbi7E0qiXC99GtHVDh5VWvdTs2LCv3wnRULtLp6ZCoToZ8qoUWMzU9PZldsKSnE'
Try using re.search
instead of re.match
, match checks for a match only at the beginning of the string, while search checks for a match anywhere in the string.
You will probably also need the re.MULTILINE
flag when creating the regex, something like this should do what you need:
re.search("Auth=(.*)", data, re.MULTILINE)
If Auth is not the last value then replace the .*
with a regex that will match up until the next value.
Andrew's answer is correct. Also, I'd mention the named groups feature. You can easily extract parts from a regular expression:
import re
text = "SID=whateverAuth=myauthvalue"
m = re.search('Auth=(?P<auth>.+)', text, re.MULTILINE)
print m.group('auth') #should print myauthvalue
Python official docs: http://docs.python.org/library/re.html
No need for regexp, loop over the lines instead and split on =
. A bit verbose perhaps...
#!/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
If the string is multiline and always in the same order (SID, LSID, Auth), you can use this:
wantedresult = originalstring.strip().split('\n')[-1].split('=')[-1]
strip
is necessary to remove the extra newline most servers include at the end of the original string.
精彩评论