Reputation: 4868
I want to find whether a given regular expression is a subset of larger regular expression. For example given a larger regular expression ((a*)(b(a*))), I want to find if a regular expression like (aab.*) or (a.*) matches to it. I am developing a program where I need to find all sub string of given length that can be formed from given regular expression.
$count=0;
$len=0;
sub match{
my $c=$_[1];
my $str=$_[0];
my $reg=$_[2];
#if($str.".*"!~/^$reg$/){
# return;
#}
if($c==$len){
if($str=~/^reg$/){
$count++;
}
return;
}
my $t=$str.'a';
&match($t,$c+1,$reg);
my $t=$str.'b';
&match($str.'b',$c+1,$reg);
}
for(<>){
@arr=split(/\s/,$_);
$len=$arr[1];
&match('a',1,$arr[0]);
&match('b',1,$arr[0]);
print $count;
}
So I thought that I would start strings of given length using recursion and when the string size reaches desired length, I would compare it to original exp. This works fine for small sub strings but runs into stack overflow for larger sub strings. So I was thinking that while generating part of string itself I would check the expression to given reg exp. But that didn't work. For above given reg exp ((a*)(b(a*))) if we compare it to partial string (aa) it will fail as the reg exp doesn’t match. So in order for it to work, I need to compare two regular expression by adding .* behind every partial sub stirng. I tried to find answer on web but was unsuccessful.
I tried the following code but naturally it failed. Can any one suggest some other approach.
if("a.*"=~/((a*)(b(a*)))/){
print match;
}
But here the first part is considered as an actual string. Can you help me how to convert code so I can compare (a.*) as a regular expression instead of string.
Upvotes: 0
Views: 163
Reputation:
I think one approach is to find the length of matched string if it can be done. For instance if you match (aab) to (aac) than you can obtain length where the matched stopped.
Now compare the position where the match stopped, if its equal to length of your string than its equivalent to regex of str(.*). I read that it can be done in some other languages but I am not sure about perl.
Upvotes: 1