我有这样一个文本:
**********************************************************************
********* *********
********* llgalfaslfjasljflksjaglajlgjlajlgfaslg *********
********* *********
********* Key1: value1 *********
********* *********
********* Key2: Thu, 06.02.2020 22:28:22 *********
********* *********
********* Key3: Sep 30 2019-02:35:32 *********
********* key4: dc960e6, ttt: 35044b5 *********
********* *********
********* Key5: Value5 *********
********* *********
********* Key6: Value6 *********
********* *********
**********************************************************************
我想写一个函数,比如:
def getKey(src,pattern,key):
...
其中:
source = the text as seen in the sample above (can be more or les )
pattern = regex pattern
key = the key I would like to get the value of
理想情况下,结果应该是src中键的值列表
例如:
Key=Key1 => [value1]
Key=Key2 => [Thu, 06.02.2020 22:28:22]
Key=Key4 => [dc960e6, ttt: 35044b5]
基本上是后面的值:当删除所有的星、空间时
我不知道如何为此创建正则表达式模式
尝试了类似于:
pattern = r'(?<=^*********).+(?=\*********$)'
希望有人能帮忙
它可以用一行程序解析,不需要regexp。输入变量为
text
相同的代码格式正确:
输出指令:
因此,您可以使用类似正则表达式的子表达式(其中
s
是整个字符串)来表示开始和结束:删除线条末端的星号和空格。在此之后,您可以使用以下命令拆分每一行:
对于不带out
:
的行,这将失败,您可以捕获它。比如:给出一个
d
,其中包含:您可以使用python-textops3,它能够执行许多字符串操作/解析:
给出:
但是如果你想得到一个特殊的kay值,就像你问的:你的函数是:
相关问题 更多 >
编程相关推荐