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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |