Hello all

I have a code which uses a pattern and checks if a string is in the correct pattern form. if it does it say yes.

if ($port111 =~ /^(\d{1,2})\/(\d{1,2})\/(\d{1,2})$/) {print "yes port +111=$port111\n";}

allowed strings are 1/1/2 2/3/15 6/10/1 etc... all others are rejected.

I also have strings like those :
ME170-5/2/8-ME172-2/2/6-ME4028
ME172-2/1/2-ME196-1/1/3-ME4002
and i would like to extract the substring that match the pattern . for example :
ME170-5/2/8-ME172-2/2/6-ME4028
would extract 5/2/8 and 2/2/6
i have tried using this code but it does not work :-(.

$string = 'ME170-5/2/8-ME172-2/2/6-ME4028'; @ports = $string =~ /^(\d{1,2})\/(\d{1,2})\/(\d{1,2})$/;

can anyone point be in the right direction?
Avi

P.s also is there a way to get an array with indexed locations of where a matching patterns were found in the string?


In reply to Extract a pattern from a string by avim1968

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.