[英]One Line Regex, match all words separated by whitespace
I am trying to create a regEx that can match the entire above line into seperate groups:我正在尝试创建一个正则表达式,可以将上面的整个行匹配到单独的组中:
Sample Message: 2019-10-07 11:13:19.75 New York America INFO Server is connected. Server Id [34] Address [127.0.0.1:54266]
示例消息:
2019-10-07 11:13:19.75 New York America INFO Server is connected. Server Id [34] Address [127.0.0.1:54266]
2019-10-07 11:13:19.75 New York America INFO Server is connected. Server Id [34] Address [127.0.0.1:54266]
EDIT: The groups are separated by multiple whitespace characters at least 2 whitespace characters but usually 3.编辑:这些组由多个空格字符分隔,至少 2 个空格字符,但通常是 3 个。
EDIT2: The bracketed terms in the message portion may not appear at all, or one may appear without the other or they may appear in a different order. EDIT2:消息部分中括号内的术语可能根本不出现,或者一个可能没有另一个出现,或者它们可能以不同的顺序出现。
Group1: 2019-10-07 11:13:19.75
第一组:
2019-10-07 11:13:19.75
Group2: New York
Group2:
New York
Group3: America
第三组:
America
Group4: INFO
第 4 组:
INFO
Group5: Server is connected. Server Id [34] Address [127.0.0.1:54266]
Group5:
Server is connected. Server Id [34] Address [127.0.0.1:54266]
Server is connected. Server Id [34] Address [127.0.0.1:54266]
Im also trying to select anything inside square brackets ([ ]) and also a special match case for IP addresses.我也在尝试 select 方括号 ([ ]) 内的任何内容,以及 IP 地址的特殊匹配案例。 This doesnt have to be included in the same regex though.
不过,这不必包含在同一个正则表达式中。 But would be nice if possible.
但如果可能的话会很好。
Ive been able to match individual groups, but I cant seem to get it all going in 1 line.我已经能够匹配各个组,但我似乎无法将其全部放在 1 行中。
Here is what I have so far:这是我到目前为止所拥有的:
Group1: /[0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{2}/
组 1:
/[0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{2}/
Group2: /(?<=\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{2}.*)\S+/
组2:/(?<=\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:
/(?<=\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{2}.*)\S+/
Group3: /(?<=\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{2}.*\S.\s{3}).?\S+/
第三组:/(?<=\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:
/(?<=\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{2}.*\S.\s{3}).?\S+/
Group4: /(?<=\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{2}(.*\S.\s{3}){2}).?\S+/
组4:/(?<=\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:
/(?<=\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{2}(.*\S.\s{3}){2}).?\S+/
Group5: /(?<=\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{2}(.*\S.\s{3}){3}).*/
第 5 组:/(?<=\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:
/(?<=\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{2}(.*\S.\s{3}){3}).*/
Any help would be greatly appreciated.任何帮助将不胜感激。
I think Liam and Thomas are right, just split by \s+
and trim the values.我认为 Liam 和 Thomas 是对的,只是用
\s+
分割并修剪值。
But I was curious about how to regex this...但我很好奇如何正则表达式...
Try this monster:试试这个怪物:
(?<date>\d{4}-\d{2}-\d{2})\s+(?<time>\d{2}:\d{2}:\d{2}\.\d{2})\s+(?<city>(?:\S|\s(??\s))*)\s+(?<country>(:?\S|\s(??\s))*)\s+(?<level>INFO|WARN|ERROR)\s+(?<message>[^\[]+\[(,<serverid>\d+)\][^\[]+\[(.<serverip>\d{1,3}\.\d{1,3}\.\d{1,3}\:\d{1,3}:\d{1,5})\])
I think you're over doing it, if you know that your seperator is at least two white spaces, juste split
by \s{2,}
:我认为你已经完成了,如果你知道你的分隔符至少是两个空格,只需用
\s{2,}
split
:
const message = `2019-10-07 11:13:19.75 New York America INFO Server is connected. Server Id [34] Address [127.0.0.1:54266]`; const groups = message.split(/\s{2,}/g) console.log(groups);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.