Jump to content

Noob Question about Parens...


ScotDiddle

Recommended Posts

Hello All,

 

I have been successfully following along with the tutorial on Regular Expressions from : http://www.brainbell.com/tutorials/php/TOC_Regex.html but I don't understand the the results I am getting using parentheses from their discussion on groupings. ( http://www.brainbell.com/tutorials/php/Grouping_And_Choice.html )

 


$in_regex = "(very){1,}";

$string1 = 'very good';
$string2 = 'very very good';
$string3 = 'very very very very very smokin\' good';

$success1 = ereg($in_regex, $string1, $match1);
$success2 = ereg($in_regex, $string2, $match2);
$success3 = ereg($in_regex, $string3, $match3);

echo 'POSIX Reg Expression : ' . $in_regex  . "<br /> <br /> \n";

echo '$string1 : ' . $string1  . "<br /> \n";
echo '$string2 : ' . $string2  . "<br /> \n";
echo '$string3 : ' . $string3  . "<br /> <br />\n";

echo '$success1 : ' . $success1  . "<br /> \n";
print_r($match1);
echo   "<br />  <br /> \n";

echo '$success2 : ' . $success2  . "<br /> \n";
print_r($match2);
echo   "<br />  <br /> \n";

echo '$success3 : ' . $success3  . "<br /> \n";
print_r($match3);
echo   "<br /> <br />  \n";

 

Returns:

 


POSIX Reg Expression : (very){1,}

$string1 : very good
$string2 : very very good
$string3 : very very very very very smokin' good

$success1 : 4
Array ( [0] => very [1] => very ) 

$success2 : 4
Array ( [0] => very [1] => very ) 

$success3 : 4
Array ( [0] => very [1] => very 

 

My question is:  Why the double entries in the $match* arrays ?

 

Thanks in advance for any and all explanations.... Google was not any help.

 

Scot L. Diddle, Richmond VA

 

 

Link to comment
Share on other sites

Per the PHP docs, if you had $regs* rather than $match*:

 

If matches are found for parenthesized substrings of pattern and the function is called with the third argument regs , the matches will be stored in the elements of the array regs .

 

$regs[1] will contain the substring which starts at the first left parenthesis; $regs[2] will contain the substring starting at the second, and so on. $regs[0] will contain a copy of the complete string matched.

 

Link to comment
Share on other sites

effigy,

 

Thanks for the reply, but changing $match* to $regs (non-suffixed ( and shuffling the order of my echo's a bit ) ) I still got the same results...

 

But, I see why the two entries... IDX => 0 is a copy of the sub-string matched, while IDX => 1 is the value found by the match...

 

So:  What's the difference between the two... ? ( Or better yet, Why are there two (or more) ) ?

 

Scot L. Diddle, Richmond VA

Link to comment
Share on other sites

The variable name doesn't matter. I mentioned it because the documentation is written against the prototype, which uses $regs for an example. It should make sense if you read the paragraph again, disregarding all variable names.

Link to comment
Share on other sites

effigy,

 

Earlier I hit POST instead of Preview... Please see my new question above, post-modify.

 

Thanks,

 

Scot L. Diddle, Richmond VA

Link to comment
Share on other sites

Perhaps a different example will help:

 

<pre>
<?php
$alphabet = join('', range('a', 'z'));
ereg('(abc).+', $alphabet, $matches);
print_r($matches);
?>
</pre>

 

...gives us...

Array

(

    [0] => abcdefghijklmnopqrstuvwxyz

    [1] => abc

)

...and, as the docs state:

 

[1] will contain the substring which starts at the first left parenthesis...[0] will contain a copy of the complete string matched.

 

Therefore, you're seeing "very" twice because your captured match is your full match. It happens to turn out that way based on your pattern, whereas mine does not--it matches more than it captures.

Link to comment
Share on other sites

effigy,

 

Thank-you so much for taking the time to help me learn this monster known as regular expressions...  Whereas the tutorial I  am using has some issues, I have been able to correct the supplied code and get the results that the text suggested I should get. The one on grouping was vague, supplied no examples, other than what to pass to the ereg function. 

 

Once I added the "Any character except newline" Special Sequence:  " . " (per your example), I was able to see the results similar to what you obtained with your alphabet example. 

 

Thanks Again.

 

Scot L. Diddle, Richmond VA

 

 


$inArray  = array("very good", "very very good", "Every Body Likes A very Good Joke");

printArray($inArray, '$inArray');

$in_regex = "(very).{1,}";

$success1 = regex_play($inArray, $in_regex);

 

Returned:

 



Array
(
    [0] => very good
    [1] => very very good
    [2] => Every Body Likes A very Good Joke
)

regex_play called to match '(very).{1,}':

Array Index 0 matches: array ( 0 => 'very good', 1 => 'very', ) "very good"
Array Index 1 matches: array ( 0 => 'very very good', 1 => 'very', ) "very very good"
Array Index 2 matches: array ( 0 => 'very Body Likes A very Good Joke', 1 => 'very', ) "Every Body Likes A very Good Joke"

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.