使用REGEX自定义令牌化
我有以下文本:
4/21/2021 11:43:32 PM 0ED4 PACKET [OUTPUT] GET
日志行的格式可能会更改,并且可能会添加更多字段,但它们都是单个单词。我只想加入日期和时间,
我想将其标记为:
['4/21/2021 11:43:32 PM','0RU4', 'PACKET', 'OUTPUT', 'GET']
我已经使用了此正则是“ \\ [| \\] | \,| \\ s+| \ w:| =
“哪个给我输出为:
['4/21/2021', '11:43:32', 'PM', '0ED4', 'PACKET', 'OUTPUT', 'GET']
我应该对正则施加什么更改,以便我将所需的输出作为一个令牌,以使我所需的输出作为一个令牌。
I have the following text:
4/21/2021 11:43:32 PM 0ED4 PACKET [OUTPUT] GET
The format of the log line may change and more fields may add into it but they are all single words. I only want to join date and time
I want to tokenize it to :
['4/21/2021 11:43:32 PM','0RU4', 'PACKET', 'OUTPUT', 'GET']
I have used this regex "\\[|\\]|\,|\\s+|\W:|=
" which gives me the output as:
['4/21/2021', '11:43:32', 'PM', '0ED4', 'PACKET', 'OUTPUT', 'GET']
What changes should I make to the regex such that I get my desired output with the entire date and time as one token.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将单个正则态度模式与
re.findall
:You could just use a single regex pattern along with
re.findall
:您也可以使用以下Python TTP模块。请参阅示例:
请参阅第一个结果的输出:
请参阅所需数据的输出:
You can also use the following python ttp module. See the example:
See the output of result first:
See the output of desired data:
为什么要打扰言论?
Why bother with regex?