AbydosGater Posted June 15, 2007 Share Posted June 15, 2007 Hey Guys, Ive been working on a PHP-Irc bot, just for practice with sockets. But a friend told me i was going about handling server messages all wrong and said i should break up incomming messages with preg_match using the following expression: /^(?:[^ ]+) +)?([a-z]+|[0-9]{3})((?: +(?:[^: ][^ ]*))*)(?: +.*))? *$/i' I get some of it, starts with :: and some other bits. But could some explain that to me please, i would be very greatful Thank you for reading, Andy Quote Link to comment Share on other sites More sharing options...
effigy Posted June 15, 2007 Share Posted June 15, 2007 Via Perl's YAPE::Regex::Explain: The regular expression: (?i-msx:^(?:[^ ]+) +)?([a-z]+|[0-9]{3})((?: +(?:[^: ][^ ]*))*)(?: +.*))? *$) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?i-msx: group, but do not capture (case-insensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ^ the beginning of the string ---------------------------------------------------------------------- (?: group, but do not capture (optional (matching the most amount possible)): ---------------------------------------------------------------------- : ':' ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- [^ ]+ any character except: ' ' (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- + ' ' (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- )? end of grouping ---------------------------------------------------------------------- ( group and capture to \2: ---------------------------------------------------------------------- [a-z]+ any character of: 'a' to 'z' (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- | OR ---------------------------------------------------------------------- [0-9]{3} any character of: '0' to '9' (3 times) ---------------------------------------------------------------------- ) end of \2 ---------------------------------------------------------------------- ( group and capture to \3: ---------------------------------------------------------------------- (?: group, but do not capture (0 or more times (matching the most amount possible)): ---------------------------------------------------------------------- + ' ' (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- (?: group, but do not capture: ---------------------------------------------------------------------- [^: ] any character except: ':', ' ' ---------------------------------------------------------------------- [^ ]* any character except: ' ' (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- )* end of grouping ---------------------------------------------------------------------- ) end of \3 ---------------------------------------------------------------------- (?: group, but do not capture (optional (matching the most amount possible)): ---------------------------------------------------------------------- + ' ' (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- : ':' ---------------------------------------------------------------------- ( group and capture to \4: ---------------------------------------------------------------------- .* any character except \n (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \4 ---------------------------------------------------------------------- )? end of grouping ---------------------------------------------------------------------- * ' ' (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- Quote Link to comment Share on other sites More sharing options...
Wildbug Posted June 15, 2007 Share Posted June 15, 2007 /^(?:[^ ]+) +)?([a-z]+|[0-9]{3})((?: +(?:[^: ][^ ]*))*)(?: +.*))? *$/i' ^ [start of string] (?: [use parentheses for grouping without capturing what's inside] : [a literal colon] ( [1. capture this] [^ ]+ [one or more characters that is anything but a space] ) + [end capture; one or more spaces] )? [end grouping; zero or one of the previous expression, starting with "(?:"] ( [2. capture this] [a-z] [one or more letters] | [...or...] [0-9]{3} [three digits] ) [end capture] ( [3. begin capture] (?: [begin grouping] + [one or more spaces] (?: [begin group] [^ :] [any character except space or colon] [^ ]* [zero or more of any character except space] ) [end group] )* [end group; zero or more of what's in the group] ) [end capture] (?: [begin group] + [one or more spaces] : [colon] (.*) [capture zero or more of anything -- greedily] )? [end group; zero or one of it] * [zero or more spaces] $ [end of string] /i [case insensitive (so a-z is actually a-zA-Z) Another post has been posted? Well, f*** it. I'm posting it anyway. Quote Link to comment Share on other sites More sharing options...
Wildbug Posted June 15, 2007 Share Posted June 15, 2007 Great Perl module, btw. Maybe PHP Freaks should build a web interface to it. Theoretically, it'd solve 25% of the problems here, but since no one reads the READ ME FIRST stickies, realistically 1% (with a 1% margin of error). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.