正则表达式小记

Posted carlvine

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了正则表达式小记相关的知识,希望对你有一定的参考价值。

import re

str = "send_time=2018-09-05 13:58:00&msa_id=TISN456795112&version=4.0.0.0&terminal_id=200001173&txn_type=01&member_id=100025773&dgtl_envlp=26a6ce887527df81fd73a9e4b87f4aca0f85b0437b99b381dbdb68147300becc6a19ca735851b9470f221e89ec2dffb742d718c5fb49f5d38f5280b0e71faecde02cdc33dd777f93400be2916d2058b70a080bccc62bb9563b36667f08b32b80c8f6ac8e8e02420b4644ad940e145a86a8c79264265c4add0f45f380cd2c4eaf&user_id=T117300110134&card_type=101&id_card_type=01&acc_info=164fc3adbe6f38863b5e292d0c4aa67073ecdb1feb09cae68365c7df8b71d4764532a8a095f99921768465a8f867635d3b4670890fcb76ba9a201ba971f520bd&signature=8398988b3a8f6869a9e2690eb5cd308b832eb3d29d755b71e37e83a75ef8689749b14122afbafe6a493112fce6614a54c11635442ede5e476ad6840ae52de8d27f0ff7b9fa5f8057c73f3056b9e63d906a186952003398e093caf7ec2a6e9bfc307ccf6e50c016d0001634777b9b1dd6a9c4c7600f0f73530aa55d52dcfda1b5"

‘‘‘取最后一个&前面的字符串‘‘‘
str2 = re.findall(r"(.*)&",str)
print(str2)
#输出,括号内为目标字符串,括号后面的内容从字符串末尾开始匹配
[‘send_time=2018-09-05 13:58:00&msa_id=TISN456795112&version=4.0.0.0&terminal_id=200001173&txn_type=01&member_id=100025773&dgtl_envlp=26a6ce887527df81fd73a9e4b87f4aca0f85b0437b99b381dbdb68147300becc6a19ca735851b9470f221e89ec2dffb742d718c5fb49f5d38f5280b0e71faecde02cdc33dd777f93400be2916d2058b70a080bccc62bb9563b36667f08b32b80c8f6ac8e8e02420b4644ad940e145a86a8c79264265c4add0f45f380cd2c4eaf&user_id=T117300110134&card_type=101&id_card_type=01&acc_info=164fc3adbe6f38863b5e292d0c4aa67073ecdb1feb09cae68365c7df8b71d4764532a8a095f99921768465a8f867635d3b4670890fcb76ba9a201ba971f520bd‘]
‘‘‘取等号左边为键放入列表‘‘‘ keys = re.findall(r"(w+)=", str, re.M) print(keys)
#输出
[‘send_time‘, ‘msa_id‘, ‘version‘, ‘terminal_id‘, ‘txn_type‘, ‘member_id‘, ‘dgtl_envlp‘, ‘user_id‘, ‘card_type‘, ‘id_card_type‘, ‘acc_info‘, ‘signature‘]
‘‘‘取等号左边为值放入列表‘‘‘ values = re.findall(r"=([^&]*)", str, re.M) print(values) #输出
[‘2018-09-05 13:58:00‘, ‘TISN456795112‘, ‘4.0.0.0‘, ‘200001173‘, ‘01‘, ‘100025773‘, ‘26a6ce887527df81fd73a9e4b87f4aca0f85b0437b99b381dbdb68147300becc6a19ca735851b9470f221e89ec2dffb742d718c5fb49f5d38f5280b0e71faecde02cdc33dd777f93400be2916d2058b70a080bccc62bb9563b36667f08b32b80c8f6ac8e8e02420b4644ad940e145a86a8c79264265c4add0f45f380cd2c4eaf‘, ‘T117300110134‘, ‘101‘, ‘01‘, ‘164fc3adbe6f38863b5e292d0c4aa67073ecdb1feb09cae68365c7df8b71d4764532a8a095f99921768465a8f867635d3b4670890fcb76ba9a201ba971f520bd‘, ‘8398988b3a8f6869a9e2690eb5cd308b832eb3d29d755b71e37e83a75ef8689749b14122afbafe6a493112fce6614a54c11635442ede5e476ad6840ae52de8d27f0ff7b9fa5f8057c73f3056b9e63d906a186952003398e093caf7ec2a6e9bfc307ccf6e50c016d0001634777b9b1dd6a9c4c7600f0f73530aa55d52dcfda1b5‘]
‘‘‘取等号左边为键,右边为值存为字典‘‘‘ dic = {i.split("=")[0]: i.split("=")[1] for i in str.split("&")} print(dic)
#输出
{‘send_time‘: ‘2018-09-05 13:58:00‘, ‘msa_id‘: ‘TISN456795112‘, ‘signature‘: ‘8398988b3a8f6869a9e2690eb5cd308b832eb3d29d755b71e37e83a75ef8689749b14122afbafe6a493112fce6614a54c11635442ede5e476ad6840ae52de8d27f0ff7b9fa5f8057c73f3056b9e63d906a186952003398e093caf7ec2a6e9bfc307ccf6e50c016d0001634777b9b1dd6a9c4c7600f0f73530aa55d52dcfda1b5‘, ‘dgtl_envlp‘: ‘26a6ce887527df81fd73a9e4b87f4aca0f85b0437b99b381dbdb68147300becc6a19ca735851b9470f221e89ec2dffb742d718c5fb49f5d38f5280b0e71faecde02cdc33dd777f93400be2916d2058b70a080bccc62bb9563b36667f08b32b80c8f6ac8e8e02420b4644ad940e145a86a8c79264265c4add0f45f380cd2c4eaf‘, ‘txn_type‘: ‘01‘, ‘card_type‘: ‘101‘, ‘member_id‘: ‘100025773‘, ‘user_id‘: ‘T117300110134‘, ‘acc_info‘: ‘164fc3adbe6f38863b5e292d0c4aa67073ecdb1feb09cae68365c7df8b71d4764532a8a095f99921768465a8f867635d3b4670890fcb76ba9a201ba971f520bd‘, ‘version‘: ‘4.0.0.0‘, ‘id_card_type‘: ‘01‘, ‘terminal_id‘: ‘200001173‘}

 









以上是关于正则表达式小记的主要内容,如果未能解决你的问题,请参考以下文章

小记linux正则表达式

JS正则[egExp]小记

正则表达式小记

正则表达式小记

sscanf使用小记

通过 Java 正则表达式提取 semver 版本字符串的片段