Ask a Question related to PERL Beginners, Design and Development.
-
Rick Bragg #1
script to test a file.
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
Rick Bragg Guest
-
can anybody test this file? (PC MXa)
http://www.hot.ee/allvee/Abloy_s2.FH11 is the file i was working on last night. when trying to open it today i´ve got an ´could not draw the doc...´... -
Test a zip file for validity
Hello all, How do I go about checking if a file is a zip file? Wrapping the zipinfo utility in php did not help since it does not return an... -
Bitmaps are displaced when opening FH9 file-I need a test file
Would someone who has reported opening FH 9/10 files in FH MX containing bitmaps that were being displaced please send me a file that displays this... -
Test for file existence
"Brian Berneker" <brianberneker@hotmail.com> wrote in message news:bf5e3v$v7j$1@news1.mountaincable.net... Good. What does it say on page 2 of... -
Best way to test for existence of a file
Hello, I was wondering what is the most bullet proof way to test for a file. I am concerned, that a program could hang when trying to test for... -
Andrew Gaffney #2
Re: script to test a file.
Rick Bragg wrote:
/\d+\.\d+\.\d+\.\d+\n?/s> 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?
--
Andrew Gaffney
Andrew Gaffney Guest
-
Wiggins D Anconia #3
Re: script to test a file.
than an> 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 moreBeware of insufficient regexes in regex clothing. The above will>> > ip address on one line. Does anyone have a sample of a simple regex to
> > accomplish this?
> /\d+\.\d+\.\d+\.\d+\n?/s
>
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?
[url]http://danconia.org[/url]
Wiggins D Anconia Guest
-
Jerry Rocteur #4
Re: script to test a file.
On Wednesday, Oct 29, 2003, at 19:43 Europe/Brussels, Wiggins d Anconia
wrote:
Doing it with plain regexp won't work in one go, you could put the>
>> than an>> 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>>>>>> 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?
>
above between (),(),() and check with $1,$2,$3 etc that you don't have
an 894..
However, you could also use Regexp::Common
Jerry
Jerry Rocteur Guest
-
Robert Citek #5
Re: script to test a file.
At 11:43 AM 10/29/2003 -0600, Andrew Gaffney wrote:Slightly more complex and slightly more robust:>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
cat <<EOF | perl -lne "print if /^(\d{1,3}\.){3}\d{1,3}$/"
hello
1.2.3.4.5
1.2.3
1.2.3.4
1.2.3.4 hello
hello 1.2.3.4
123.123.123.123
1234.123.123.123
EOF
Regards,
- Robert
Robert Citek Guest
-
Brian Gerard #6
Re: script to test a file.
And the clouds parted, and Wiggins d Anconia said...
Good point.>
>> than an> > 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>> >> > > 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?
>
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
[url]www.google.com[/url] - 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 Gerard I'm writing an unauthorized autobiography. |
| First initial + 'lists' |
| at technobrat dot com |
\_________________________________________________ _____________________/
Brian Gerard Guest



Reply With Quote

