in reply to Regex partial/leading match

I'm not sure this works for you, but it might: Just try all the shorter regexes if they're valid.
#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; my $bob = 'bob'; sub match { my ($string, $regex) = @_; say $string, "\t", $regex; my @chars = split //, $regex; for my $pos (1 .. $#chars) { my $re_part = join q(), @chars[0 .. $pos]; return 1 if eval { $string =~ /^($re_part)$/ and length $1 }; } return 0 } for my $regex ( 'bobly', 'bo*[a-z].', 'bob', 'bo(x)?bcd', 'fred', 'o*', ) { say match('bob', $regex); }
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: Regex partial/leading match
by raymorris (Novice) on Dec 31, 2015 at 23:21 UTC
    Thank you, unfortunately that works only for simple strings, not regexes. Consider:
    'bated' =~ /^bat{3}/; false 'bated' =~ /^bat/; true
    I'm trying to look at the beginning of a string to know if it can potentially match a regex, without knowing the full string. Which is of course different from making a completely different regex with substr($regex.

    (Edited for bad examples)