manrooster Posted February 10, 2010 Share Posted February 10, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/191572-regular-expressions-ip-search/ Share on other sites More sharing options...
Catfish Posted February 10, 2010 Share Posted February 10, 2010 http://au2.php.net/manual/en/regexp.reference.backslash.php \D any character that is not a decimal digit What are you using in the PHP to use the regexp? preg_match() or something preg_ ? Quote Link to comment https://forums.phpfreaks.com/topic/191572-regular-expressions-ip-search/#findComment-1009887 Share on other sites More sharing options...
teamatomic Posted February 10, 2010 Share Posted February 10, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/191572-regular-expressions-ip-search/#findComment-1009890 Share on other sites More sharing options...
cags Posted February 10, 2010 Share Posted February 10, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/191572-regular-expressions-ip-search/#findComment-1009973 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.