Jump to content

Preg_match expression, dont understand it.


AbydosGater

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/55741-preg_match-expression-dont-understand-it/
Share on other sites

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

/^(?:[^ ]+) +)?([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.  :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.