Jump to content

Can someone help me figure out what this does?


Hi I Am Timbo

Recommended Posts

I have a regular expression and I am reading tutorials on it, but it is really taking me a long time to figure out what this does:

 

/^([^\s]+)\s+(.+)\s+(.*\d+)\s+([^\s\d]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+(.*[^\s]+)\s+([^\s]+)$/

 

If someone could help me, that would be great.  I'm trying to break it down, but I don't understand it all.  What is \s?

Link to comment
Share on other sites

Here's some help; see these resources for more information.

/ ### Starting delimiter

^ ### BOL (Beginning of Line)

([^\s]+) ### Capture one or more instances of non-whitespace; this is better written as (\S+)

\s+ ### One or more instances of whitespace.

(.+) ### One or more instances of any character excluding a new line.

\s+

(.*\d+) ### Capture zero or more instances of any character excluding a new line, followed by one or more digits.

\s+

([^\s\d]+)

\s+

([^\s]+)

\s+

([^\s]+)

\s+

([^\s]+)

\s+

(.*[^\s]+)

\s+

([^\s]+)

$ ### Match at EOL or before a new line.

/ ### Ending delimiter

Link to comment
Share on other sites

That really helps a lot.  Let me see if I can get the rest of this down then:

 

/ ### Starting delimiter

^ ### BOL (Beginning of Line)

([^\s]+) ### Capture one or more instances of non-whitespace; this is better written as (\S+)

\s+ ### One or more instances of whitespace.

(.+) ### One or more instances of any character excluding a new line.

\s+

(.*\d+) ### Capture zero or more instances of any character excluding a new line, followed by one or more digits.

\s+

([^\s\d]+) ### Capture one or more instances of non-whitespace, followed by one or more digits

\s+

([^\s]+)

\s+

([^\s]+)

\s+

([^\s]+)

\s+

(.*[^\s]+) ### Capture zero or more instances of any character excluding a new line, followed by one or more instances of non-whitespace; this is better written as (\S+)

\s+

([^\s]+)

$ ### Match at EOL or before a new line.

/ ### Ending delimiter

 

 

 

If I put that in

 

preg_match($regex, $token, $array);

 

it will do this if it is matched:

$array[] = $token;

 

Is that correct?

Link to comment
Share on other sites

Almost. The entire match comes first, followed by any captured ones. If you want to keep the comments, you'll have to remove the last one after the ending delimiter and add the proper modifier. Something like this:

$regex = '/

pattern and comments here

/x';

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.