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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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