G'day choeppner,

In my opinion, a regex is not the right tool for this job. The string-related functions length, index and substr can provide all the functionality you need. I'd also expect them to be much faster than any regex solution (Benchmark against any you receive).

Here's my test code (with some additional test data):

#!/usr/bin/env perl use strict; use warnings; use Test::More; my @tests = ( [qw{ABC DFGABCKBG ABC}], [qw{ABC DFGAXBHCY AXBHC}], [qw{ABC DFGAXBHY AXB}], [qw{ABC DFGCBAKBG AKB}], [qw{ABC DFGCBAKbG A}], ['', 'DFGCBAKbG', ''], ['ABC', '', ''], [qw{ABC AXBHCY AXBHC}], [qw{ABC DFGAXBHC AXBHC}], [qw{abc DFGABCKBG}, ''], ); plan tests => scalar @tests; for my $test (@tests) { my ($start, $end) = (-1, -1), my ($rfid, $ocr, $exp) = @$test; for (0 .. length($rfid) - 1) { my $pos = index $ocr, substr($rfid, $_, 1), $end + 1; next if $pos == -1; $start = $pos if $start == -1; $end = $pos; } my $got = $start == -1 ? '' : substr $ocr, $start, $end - $start + + 1; is($got, $exp, "Find '$rfid' in '$ocr' as '$exp'"); }

Output:

1..10 ok 1 - Find 'ABC' in 'DFGABCKBG' as 'ABC' ok 2 - Find 'ABC' in 'DFGAXBHCY' as 'AXBHC' ok 3 - Find 'ABC' in 'DFGAXBHY' as 'AXB' ok 4 - Find 'ABC' in 'DFGCBAKBG' as 'AKB' ok 5 - Find 'ABC' in 'DFGCBAKbG' as 'A' ok 6 - Find '' in 'DFGCBAKbG' as '' ok 7 - Find 'ABC' in '' as '' ok 8 - Find 'ABC' in 'AXBHCY' as 'AXBHC' ok 9 - Find 'ABC' in 'DFGAXBHC' as 'AXBHC' ok 10 - Find 'abc' in 'DFGABCKBG' as ''

See also: Test::More

— Ken


In reply to Re: OCR matching regex by kcott
in thread OCR matching regex by choeppner

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.