Is there any way in Perl to match the leading part of a regex? I see that in C it can be done with PCRE, but I'd like to do it in Perl. I am attempting to determine whether a string matches the BEGINNING of an arbitrary regular expression. In other words, is it true that "it matches so far, we'll have to look at the rest of the string to see if the entire regex matches". A few examples might make it more clear, using the fake operator ^~ to mean "leading match":

'bob' ^~ /^bobby/ = true 'bob' ^~ /^fred/ = false 'bob' ^~ /^bo*[a-z]./ = true 'bob' ^~ /^ch*/ = false

It only needs to work for regular expression which are anchored at the beginning. Obviously if the regex isn't anchored to the beginning ANY string could match, because:

/foo/ = /.*foo.*/ therefore: 'bob' ^~ /foo/ = true

Any ideas on how to do this? The practical application is that I have many regexes such as this:

/^c:\\Windows\\Program Files\\blah[0-9]\\setup.exe/

Walking the drive, if I come to c:\Windows\Program Files\, that is a leading match, so I SHOULD recurse into the directory to see if it contains blah[0-9]\ On the other hand, I should not recurse into c:\Temp, because nothing starting with "c:\Temp\" can ever match ^c:\\windows\\Program ...

The naive/wrong (and current) implementation is to split the regex on \\ and match directory names. That obviously fails in many cases, such as the regex /windows(\\system32)?\\bob/ .

It looks like PCRE can do it in C, but I'd like to do it in Perl: http://www.pcre.org/current/doc/html/pcre2partial.html

Any ideas?

In reply to Regex partial/leading match by raymorris

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.