My snippet reads the lines of text from the <DATA> filehandle. You didn't mention where the strings are coming from, so you'll have to adjust accordingly. The regexp needs to be free to match anywhere in the string, so the ^ and $ anchors were hurting you.

my $re = qr{ ( (?: \d{1,2}/ ){2} \d{1,2} ) }x; while( my $line = <DATA> ) { while( $line =~ m/ $re /gx ) { print "Line: $.\t Column: ", $-[0], "\tMatched: $1\n"; } } __DATA__ ME170-5/2/8-ME172-2/2/6-ME4028 ME172-2/1/2-ME196-1/1/3-ME4002

It's up to you to decide what information you would like to push onto an array. Perhaps instead of the print, you could use, push @array, [ $., $-[0], $1 ];

There are a lot of funny looking variables there: $. tells you the line number most recently read from a file. $-[0] tells you the match position of $1. And $1 holds what matched inside the first set of capturing parens within the regular expression.


Dave


In reply to Re: Extract a pattern from a string by davido
in thread 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.