Hi all, I'm trying to learn regexes but they look really spooky to me at this point πŸ˜› Never worked with them before. What I'm trying to get done is to have a regex check a page I obtained through file_get_contents() to check for IP AddressπŸ˜›ort occurances. E.g. 81.52.102.63:1044.

I tried this but without luck.

$content = file_get_contents('myfile.php');
    $search = ereg('[0-9]{3}.[0-9]{3}.[0-9]{3}.[0-9]{3}:[0-9]{5}',$content,$match);
    foreach ($match as $result) {
      echo $result . "<br>\n";
    }

But as you guessed this is not working. I just get a "<br>\n" and that's it. I assume it is my noobie regex, any suggestions ?

    make sure you are using the correct type of regular expressions

    ereg is different syntax than preg so make sure the examples you are learning from are the right type

    keep in mind that '.' is a special character meaning "match any single character"
    you must escape some characters

    . matches a single period

    {3} means to match only 3 of something
    {,3} means the same as {0,3} : match up to three
    {1,3} is probably what you want, from one to three characters

    () groups things together
    ? means 0 or 1, which means

    :[0-9]{5} must include a 5 digit port
    and
    (:[0-9]{1,5})? may include the group of chars that represent a port of up to 5 digits

    regex is hard to learn in the first place.. but once you wrap your head around it do you will find it very very useful

      So basically you will need this:

       $content = file_get_contents('myfile.php');
      if(preg_match('/^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3}):([0-9]{1,5})$/', $content, $match)) {
        foreach ($match as $result) {
          echo $result . "<br>\n";
        }
      } else {
        echo "Not an IP address!<br>\n";
      }

        Thanks for the info guys. mjax: I tried what you said, but that just returns a blank page and I am very sure that there are IP:port occurances on my page. Any additions to that regex ?

          Yeah, remove the ^ and $ from the pattern (first and last characters). Also, I don't see a need to break down the IP by each block, so I'd use a pattern like this:

          if(preg_match('/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\:[0-9]{1,5}/', $content, $match)) { 
            Write a Reply...