Jump to content

php regular expression


dotolee

Recommended Posts

Hi there.  I have the following php code:

 

<?php
  echo 'starting regex test<BR>';
      $data = "c799 1 000ffe-fcc811 1 000ffe-fcc86f 1 000ffe-fcc898 1 000ffe-fcc92d 1 000ffe-fcc934 1 000ffe-fcc965 1 000ffe-fcc96b";
      
      $pattern="/([a-fA-F0-9]){6}-([a-fa-fA-F0-9]){6}$/i";
      echo "the pattern we are using is:".$pattern."<BR>";
      if (preg_match($pattern,$data))
      {
$matches = preg_match($pattern,$data, $matches);

echo 'Matches we found:'. print_r($matches, true);
      } else     {
echo "No, the mac address is wrong";     
}
      
?>

I'm just wondering why it only finds one match....

also, what is the proper way to define a hex value in regular expressions?

thank you.

Link to comment
Share on other sites

Check the manual for preg_match (), or do you mean that the second half of the RegExp isn't triggering?

 

As for the proper way, if you're searching a string that may contain something that looks like a hex value, then you've done it correctly.

That said, I don't quite understand why you've made the RegExp case insensitive, but still provided both upper and lower case letters for the character groups. (Or doubled the lower case range for the second character group.) Seems you might be a bit confused on how Regular Expressions actually work, and in that case I strongly recommend reading up on them.

Link to comment
Share on other sites

I chnaged the code to read:

 

 

   
echo 'starting regex test<BR>';
      $data = "c799 1 000ffe-fcc811 1 000ffe-fcc86f 1 000ffe-fcc898 1 000ffe-fcc92d 1 000ffe-fcc934 1 000ffe-fcc965 1 000ffe-fcc96b";
      
      $pattern="/([a-f0-9]){6}-([a-fa-f0-9]){6}$/i";
      echo "the pattern we are using is:".$pattern."<BR>";
      if (preg_match_all($pattern,$data,$matches, PREG_SET_ORDER))
      {
//echo 'Matches we found:'. print_r($matches, true);
print_r($matches[0][0]);
echo"<BR>";
print_r($matches[0][1]);

      } else     
      {
echo "No, the mac address is wrong";     
      }
      
      

And this is the output i'm seeing on the webpage:

     

starting regex test

the pattern we are using is:/([a-f0-9]){6}-([a-fa-f0-9]){6}$/i

000ffe-fcc96b

e

 

I guess i don't understand why it's printing the "e"?

I've tried reviewing the documentation found here:

http://ca2.php.net/manual/en/function.preg-match-all.php

and i do understand the results  i get with their telephone example...

Is there any way i can get a count of the number of matches it found?

 

Thanks

Link to comment
Share on other sites

First of all it seems you skipped my post entirely, or at least didn't follow up on it. So I recommend (re-)reading it before continuing on with this post.

 

Now that you've done that, you should take a look at the RegExp reference and pay really close attention to the details. Remember that every single character in a regular expression has a very specific meaning, including the order of which they are used, and that regular expressions have a lot of meta-characters.

In short, there are multiple problems with your RegExp. Three more in additions to the ones I've mentioned before, in fact. All of which would have been avoided by properly reading the tutorials, planning and paying attention to the details.

Link to comment
Share on other sites

ChristianF - Thanks for the response.  I just want to say that I realize it might be frustrating to reply to folks who don't do their homework.  Having said that, I did take your suggestion and I read up on the function as I mentioned in my post.  I reviewed their telephone example and had additional questions.

Sometimes, its not that people are being lazy by just asking questions.  They may have actually tried to do their homework but need an additional explanation, and hence decide to post a question.

 

Link to comment
Share on other sites

Ok, good to know. :)

Still, you didn't follow up on the second half of my post, which is what triggered the first line in my previous post.

 

That said, I can give you a couple more hints: Take a look at the placement of the parentheses, and read out loud what each single element of the RegExp does. You should get an a-ha experience then.

Link to comment
Share on other sites

<?php
echo 'starting regex test<BR>';

$data = "c799 1 000ffe-fcc811 1 000ffe-fcc86f 1 000ffe-fcc898 1 000ffe-fcc92d 1 000ffe-fcc934 1 000ffe-fcc965 1 000ffe-fcc96b";

$pattern="/([a-f0-9]){6}-([a-f0-9]){6}/";
echo 'Looking for ' . $pattern . ' in ' . $data;
if (preg_match_all($pattern,$data,$matches, PREG_SET_ORDER)) {
      echo '<pre>';
      print_r($matches);
      echo '</pre>';
} else {
echo "No, the mac address is wrong";     
}

 

I have removed the $ and the i at the end, as well as removing the extra a-f in the second match.

 

If you run it, you will start to see what PHP is doing.

 

Array
(
    [0] => Array
        (
            [0] => 000ffe-fcc811
            [1] => e
            [2] => 1
        )

    [1] => Array
        (
            [0] => 000ffe-fcc86f
            [1] => e
            [2] => f
        )

    [2] => Array
        (
            [0] => 000ffe-fcc898
            [1] => e
            [2] => 8
        )

    [3] => Array
        (
            [0] => 000ffe-fcc92d
            [1] => e
            [2] => d
        )

    [4] => Array
        (
            [0] => 000ffe-fcc934
            [1] => e
            [2] => 4
        )

    [5] => Array
        (
            [0] => 000ffe-fcc965
            [1] => e
            [2] => 5
        )

    [6] => Array
        (
            [0] => 000ffe-fcc96b
            [1] => e
            [2] => b
        )

)

 

$matches is an array of arrays. Each sub-array has three values, the entire matched string, the last matched character in the first bracket, and the last matched character in the last bracket. If you were to remove the brackets, it would only return the matched string.

 

<?php
echo 'starting regex test<BR>';

$data = "c799 1 000ffe-fcc811 1 000ffe-fcc86f 1 000ffe-fcc898 1 000ffe-fcc92d 1 000ffe-fcc934 1 000ffe-fcc965 1 000ffe-fcc96b";

$pattern="/[a-f0-9]{6}-[a-f0-9]{6}/";
echo 'Looking for ' . $pattern . ' in ' . $data;
if (preg_match_all($pattern,$data,$matches, PREG_SET_ORDER)) {
      echo '<pre>';
      print_r($matches);
      echo '</pre>';
} else {
echo "No, the mac address is wrong";     
}

 

Array
(
    [0] => Array
        (
            [0] => 000ffe-fcc811
        )

    [1] => Array
        (
            [0] => 000ffe-fcc86f
        )

    [2] => Array
        (
            [0] => 000ffe-fcc898
        )

    [3] => Array
        (
            [0] => 000ffe-fcc92d
        )

    [4] => Array
        (
            [0] => 000ffe-fcc934
        )

    [5] => Array
        (
            [0] => 000ffe-fcc965
        )

    [6] => Array
        (
            [0] => 000ffe-fcc96b
        )

)

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.