I stared with below. It won't fulfill. It can catch ipv4
 [\(\d+\.\d+\.\d+\.\d+ \<\)]+

One big character class like this is not the way to match structured substrings. This class is equivalent to  [()\d. +<]+ which will match '12345' or '.+.+.+.' or '()(()((()' as well as any dotted decimal IPv4 address, valid or otherwise. Please see perlre and perlretut on the general topic of "character class(es)" (and also perlrecharclass).

The following works (or seems to) for IPv4 dotted decimal addresses only, but it should be fairly easy to extend the  $rx_IPv4_dd regex to include IPv6 using the latest Regexp::Common::net. Needs Perl version 5.10+ for the  (?|pattern) "branch reset" extension. Needs many more test cases.

c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; use Test::More 'no_plan'; use Test::NoWarnings; ;; use Regexp::Common qw(net); ;; my $rx_IPv4_dd = qr{ (?<! \d) $RE{net}{IPv4} (?! \d) }xms; my $rx_arrow = qr{ \s+ < \s+ }xms; ;; VECTOR: for my $ar_vector ( 'all these contain one or more IPv4 addresses', [ 'x (209.85.208.68) x (172.217.194.27 < 123.23.34.45) x', '209.85.208.68', '172.217.194.27', '123.23.34.45', ], [ ' < 209.85.208.68) x (172.217.194.27 < 123.23.34.45) x', '172.217.194.27', '123.23.34.45', ], [ 'x (1.2.3.4) x (9.8.7.6 < 5.6.7.8) x', '1.2.3.4', '9.8.7.6', '5.6.7.8', ], 'none of these should match', [ '', ], [ 'x', ], [ '123', ], [ '209.85.208.68 (999.12.23.999) (12.23.34.45 98.76.54.32)', ], ) { if (not ref $ar_vector) { note $ar_vector; next VECTOR; } ;; my ($str, @expected) = @$ar_vector; ;; my @got = $str =~ m{ (?| (?: [(] ($rx_IPv4_dd) (?= (?: $rx_arrow $rx_IPv4_dd)? [)] ) ) | (?: \G (?! \A) $rx_arrow ($rx_IPv4_dd) [)] ) ) }xmsg; is_deeply \@got, \@expected, qq{'$str' -> (@expected)}; } ;; done_testing; ;; exit; " # all these contain one or more IPv4 addresses ok 1 - 'x (209.85.208.68) x (172.217.194.27 < 123.23.34.45) x' -> (209 +.85.208.68 172.217.194.27 123.23.34.45) ok 2 - ' < 209.85.208.68) x (172.217.194.27 < 123.23.34.45) x' -> (172 +.217.194.27 123.23.34.45) ok 3 - 'x (1.2.3.4) x (9.8.7.6 < 5.6.7.8) x' -> (1.2.3.4 9.8.7.6 5.6.7 +.8) # none of these should match ok 4 - '' -> () ok 5 - 'x' -> () ok 6 - '123' -> () ok 7 - '209.85.208.68 (999.12.23.999) (12.23.34.45 98.76.54.32)' -> () 1..7 ok 8 - no warnings 1..8


Give a man a fish:  <%-{-{-{-<


In reply to Re: Regex to catch IPV4 and IPV6 whenever ip appears withing brackets by AnomalousMonk
in thread Regex to catch IPV4 and IPV6 whenever ip appears withing brackets by theravadamonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.