goldgriff has asked for the wisdom of the Perl Monks concerning the following question:

I am matching an ip address within a string. I am using a regex from this site, and I am getting mixed results.

Regex =

m/([01]?\d\d?|2[0-4]\d|25[0-5]) \. ([01]?\d\d?|2[0-4]\d|25[0-5]) \. ([01]?\d\d?|2[0-4]\d|25[0-5]) \. ([01]?\d\d?|2[0-4]\d|25[0-5])(.*)/x

Text --------------------> Results

'23.165.2.40' '23.165.2.40' '(23.165.2.40)' '23.165.2.40' '[23.165.2.40]' '23.165.2.4' '{23.165.2.40}' '23.165.2.4'

Thanks in advance.

Replies are listed 'Best First'.
Re: IP Number Regex
by schumi (Hermit) on Jul 23, 2001 at 01:24 UTC
    I tried your regex, and it worked for me. Maybe there is something else in the code around it which messes it up. But it seems to me that for your purposes you could leave out the (.*) bit, maybe that does the trick.

    Else you might ask japhy about this, he'll surely be able to tell you more.

    Hope this helps.

    --cs

Re: IP Number Regex
by snowcrash (Friar) on Jul 23, 2001 at 09:37 UTC
    ok, your regex seems to come straight from the pages of the Owls Book. why did you insert that dot star at the end? it want alter the match in any way, it just captures the rest of the string in $5.
    one problem you might have a look into is the context of your matching. for example, strings like "2243423423.23.23.2312blabla34" or "1.2.3.4.5.6.7.8.9" will match even though they probably don't contain what you want to match as a valid ip address.


    snowcrash //////
Re: IP Number Regex
by petdance (Parson) on Jul 23, 2001 at 04:53 UTC
    I'd keep it simple. /(\d{1,3}\.){3}\d{1,3}) should be fine.

    Are you pulling the data from someplace with a lot of pathological potential hits?

    xoxo,
    Andy
    --
    <megaphone> Throw down the gun and tiara and come out of the float! </megaphone>

Re: IP Number Regex
by miyagawa (Chaplain) on Jul 23, 2001 at 09:54 UTC
Re: IP Number Regex
by converter (Priest) on Jul 23, 2001 at 09:00 UTC

    It might help if you showed the code you're using to assign the values to the test strings and process the $DIGIT variables after the pattern match. It's always possible that the strings you're attempting to match are not what you intended, or that a side-effect of the way you're using the data is causing truncation. Just a thought.

    conv