Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to parse the Twitch IRC chat into a more readable way. I have never used Regex and am not sure how to go about this (even after reading tons of tutorials.)

This is the raw output:

:nick!nick@nick.tmi.twitch.tv PRIVMSG channel :

I would like two regex's to parse the nick and message to be used individually, thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
253 views
Welcome To Ask or Share your Answers For Others

1 Answer

Regex is not your solution for this problem. If you really want to go down this road (but don't - keep reading!), then you can use something like this for the entire message:

:(?<nick>[^ ]+?)!(?<user>[^ ]+?)@(?<host>[^ ]+?) PRIVMSG (?<target>[^ ]+?) :(?<message>.*)

There's capture groups defined on the nick, username, hostname, channel, and message. I've not tested that, and it'll fail miserably on pretty much every other IRC event, and there will be ways to break it or get around the matching as it's the wrong sort of grammar tool for IRC: it's like hammering in nails with a screwdriver - while it works some of the time, it's harder than it needs to be, and can be made to work better with a lot of time, effort, and pain; why would you not use a hammer?

A much better solution is to simply parse the message. The IRC specs in RFC1459 and RFC2812 give some pretty useful hints here. My advice from experience is to split on " :" (space then colon) - this is the last parameter of the message, then split the first half by spaces. If the first entry in your list starts with a space, split it again by ! and @ to get the parts of the nickname/username/hostname tuple. Follow this method, and you'll have the base to a much more robust and extensible parser than one you could ever build using regular expressions.

If you're doing this as a learning exercise, great! If not, you probably want to consider using a pre-built library to handle all the IRC communication for you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...