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?

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

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?

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

Almost. The entire match comes first, followed by any captured ones.

 

I guess I don't know what you mean by this.  Does this mean that the array would look like

 

$array[0] = $token;

$array[1] = first part of $token;

$array[2] = second part of token;???

 

 

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.