Re: script to test a file.

2003-10-29 Thread Brian Gerard
And the clouds parted, and Wiggins d Anconia said...
> 
> 
> > Rick Bragg wrote:
> > > Hi,
> > > 
> > > I want to write a script that will test the contents of a file.
> > > 
> > > The file being tested will pass only if it contains nothing more
> than an 
> > > ip address on one line.  Does anyone have a sample of a simple regex to 
> > > accomplish this?
> > 
> > /\d+\.\d+\.\d+\.\d+\n?/s
> > 
> 
> Beware of insufficient regexes in regex clothing.  The above will
> certainly find one or more digits followed by a dot, followed by one or
> more digits followed by a dot, etc.  But it does *NOT* match an IP
> address...
> 
> /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
> 
> Is at least closer, but it is still not sufficient, as IP addresses are
> bounded, I believe, at 255 so 336.47.894.0, will match the above, but is
> not a valid IP address.
> 
> How strict do you need to be, how sure are you about the data in the files?
> 

Good point.

At it's most basic, to match any valid IPv4 address alone on a line, I would
use
/\A((1?\d{1,2}|2([0-4]\d|5[0-5]))\.){3}(1?\d{1,2}|2([0-4]\d|5[0-5]))\z/

Which will match a string containing only a dotted quad from 0.0.0.0 to
255.255.255.255, inclusive.  Note that I'm assuming the string in
question contains the _entire_ contents of the file.

A couple of things to note, though:
* A valid IP is actually any 32 bit integer from 0 to 4294967295 (try
  putting 3639555427 in Mozilla (or supposedly any other well-behaved
  browser... in other words, _not_ IE)... it will take you to Google
  because that number is actually 216.239.53.99 - the address for
  www.google.com - in base-10).
* This pattern doesn't exclude RFC1918 reserved addresses (10/8,
  172.16/12, and 192.168/16), or multicasts.
* It doesn't take netmasks into account.  1.2.3.255/16 is a valid
  host address, while 1.2.3.255/24 is not (it's a broadcast).

Taking these sorts of things into account could make for a bit hairier
solution.  ;)  Caveat user.



  /~~\
 | Brian GerardI'm writing an unauthorized autobiography. |
 | First initial + 'lists'|
 | at technobrat dot com  |
  \__/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: script to test a file.

2003-10-29 Thread Robert Citek
At 11:43 AM 10/29/2003 -0600, Andrew Gaffney wrote:
Rick Bragg wrote:
Hi,
I want to write a script that will test the contents of a file.
The file being tested will pass only if it contains nothing more than an 
ip address on one line.  Does anyone have a sample of a simple regex to 
accomplish this?
/\d+\.\d+\.\d+\.\d+\n?/s
Slightly more complex and slightly more robust:

cat <
Regards,
- Robert
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Re: script to test a file.

2003-10-29 Thread Jerry Rocteur
On Wednesday, Oct 29, 2003, at 19:43 Europe/Brussels, Wiggins d Anconia 
wrote:



Rick Bragg wrote:
Hi,

I want to write a script that will test the contents of a file.

The file being tested will pass only if it contains nothing more
than an
ip address on one line.  Does anyone have a sample of a simple regex 
to
accomplish this?
/\d+\.\d+\.\d+\.\d+\n?/s

Beware of insufficient regexes in regex clothing.  The above will
certainly find one or more digits followed by a dot, followed by one or
more digits followed by a dot, etc.  But it does *NOT* match an IP
address...
/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/

Is at least closer, but it is still not sufficient, as IP addresses are
bounded, I believe, at 255 so 336.47.894.0, will match the above, but 
is
not a valid IP address.

How strict do you need to be, how sure are you about the data in the 
files?

Doing it with plain regexp won't work in one go, you could put the 
above between (),(),() and check with $1,$2,$3 etc that you don't have 
an 894..

However, you could also use Regexp::Common

Jerry

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Re: script to test a file.

2003-10-29 Thread Wiggins d Anconia


> Rick Bragg wrote:
> > Hi,
> > 
> > I want to write a script that will test the contents of a file.
> > 
> > The file being tested will pass only if it contains nothing more
than an 
> > ip address on one line.  Does anyone have a sample of a simple regex to 
> > accomplish this?
> 
> /\d+\.\d+\.\d+\.\d+\n?/s
> 

Beware of insufficient regexes in regex clothing.  The above will
certainly find one or more digits followed by a dot, followed by one or
more digits followed by a dot, etc.  But it does *NOT* match an IP
address...

/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/

Is at least closer, but it is still not sufficient, as IP addresses are
bounded, I believe, at 255 so 336.47.894.0, will match the above, but is
not a valid IP address.

How strict do you need to be, how sure are you about the data in the files?

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: script to test a file.

2003-10-29 Thread Andrew Gaffney
Rick Bragg wrote:
Hi,

I want to write a script that will test the contents of a file.

The file being tested will pass only if it contains nothing more than an 
ip address on one line.  Does anyone have a sample of a simple regex to 
accomplish this?
/\d+\.\d+\.\d+\.\d+\n?/s

--
Andrew Gaffney
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


script to test a file.

2003-10-29 Thread Rick Bragg
Hi,

I want to write a script that will test the contents of a file.

The file being tested will pass only if it contains nothing more than an 
ip address on one line.  Does anyone have a sample of a simple regex to 
accomplish this?

Thanks
Rick
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]