Jump to content

Regular expressions IP search


manrooster

Recommended Posts

Hello, this thing searches for 1 IP inside a file.

 

Example.

 

root/ipaddresses.php  - this file has a bunch of IP addresses.

 

root/index.php  - this file has a code to search for IP addresses inside root/ipaddresses.php.

 

This is the regex code to find an IP address.

 

#\r\n?|\n#

 

I found the code above on the web on some site that doesn't explain it. I've also google'd that line of code and I got no results.

 

 

 

My point is to get that code to find any IP address plus anything I tell it to.

What the heck do you mean? You ask.

 

 

 

 

 

 

 

 

Example.

127.0.0.1
123.1334.342.23
434.232.432.134

 

Those three IP addresses above are something that the regular expression code will find. It will not find an IP address like this below.

127.0.0.1h
123.1334.342.23t
b434.232.432.134

 

Did you notice those letters at the end or beginning of the IPs? I want the regular expression to be able to find 1 single letter at the front or the begging of the code. The letter doesn't really matter as long as it can read it in that format; the letter and IP.

Link to comment
Share on other sites

Where ever you got that regex at you should read better or pay more attention to what you grab. That regex will match a windows line ending or a unix line ending.

 

This preg_match will put the IP from a line as you loop through a file into the array match.

 

preg_match('/^[A-Za-z]?[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}[A-Za-z]?$/',$line,$match[]);

 

 

HTH

Teamatomic

Link to comment
Share on other sites

Whilst teamatomic's pattern may work, I don't personally see the point in anchoring the pattern at the start and end of the lines and match letters before and after, may as well simplify it to something more like...

 

'#[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}#'
// or even
'#(?:[0-9]{1,3}\.){3}[0-9]{1,3}#'

 

Of course this will still pick up values that aren't valid IPs so shouldn't be used for validation, but for scraping purposes they should be fine.

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.