简体   繁体   中英

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]

EDIT: The groups are separated by multiple whitespace characters at least 2 whitespace characters but usually 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.

Group1: 2019-10-07 11:13:19.75

Group2: New York

Group3: America

Group4: INFO

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. 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.

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}/

Group2: /(?<=\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+/

Group4: /(?<=\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}).*/

Any help would be greatly appreciated.

I think Liam and Thomas are right, just split by \s+ and trim the values.

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

I think you're over doing it, if you know that your seperator is at least two white spaces, juste split by \s{2,} :

 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM