简体   繁体   English

一行正则表达式,匹配由空格分隔的所有单词

[英]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})\])

here: regex101这里:正则表达式101

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.

相关问题 正则表达式匹配特定单词组之后由一个点分隔的单词,直到空格 - Regex to match words separated by one dot after particular set of words until whitespace 正则表达式匹配行,所有单词都以大写开头 - Regex match line with all words starting in uppercase 正则表达式匹配除AND,OR和NOT之外的所有单词 - regex to match all words but AND, OR and NOT 使用正则表达式匹配由标点符号分隔的单词 - Match words separated by punctuation characters using regex Javascript正则表达式以匹配由分隔符分隔的单词 - Javascript regex to match words separated by a spaced delimiter 正则表达式匹配所有单词,但以特殊字符开头和结尾的单词除外 - Regex to match all words but the one beginning and ending with special chars JavaScript正则表达式可匹配2个单词和一个长度受限制的空白字符 - JavaScript Regex to match 2 words and an whitespace character with length limitations 用单个正则表达式匹配用给定单词分隔的给定单词分隔的列表? - Match listing with given words separated by given list of separators with single regex? 什么是匹配字母数字 6 字符单词的正则表达式,用空格或逗号分隔 - What is the regex to match alphanumeric 6 character words, separated by space or comma 正则表达式匹配以破折号分隔的单词或仅匹配单个单词 - RegEx to match either words separated by dash or just a single word
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM