Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert variable to regex

Status
Not open for further replies.

LawnBoy

MIS
Mar 12, 2003
2,881
Terrible subject title, not sure what to call my problem...
I'm constructing a search function. Works great so far, but I want to make it case-insensitive. What I've got is along the lines of
Code:
use Win32::GUI;
use File::Find::Rule;

# $main is the handle to my primary window
my $Pattern = $main->AddTextfield(
        -height => 20,
        -width  => 100,
        -text   => "*",
);

my $found_files = find(
        name => ($Pattern->Text),
        maxdepth => "$depth",
        exec => \&GotMatch,
        in => ($searchdir),
	);
According to the docs on File::Find::Rule, "name" can be a regex, and I know I can make a regex case-insensitive using the /i operator. I just have no clue how to express $Pattern->Text as a regex, and guessing in the dark isn't helping.

Can somebody buy me a clue?
Thanks.
 
*Grabs Friedl of the shelf*
Code:
my $foo = "Hello World";

my $obj1 = qr/world/;
my $obj2 = qr/(?i:world)/;

$foo =~ /$obj1/ and print "test 1 ok\n"; [COLOR=blue]# Won't work[/color]
$foo =~ /$obj2/ and print "test 2 ok\n"; [COLOR=blue]# Might just work because of (?i:) in regex[/color]
"Some flavors also support (?i:...) and (?-i:...), which turn on and off case-insensitive matching for the subexpression enclosed
 
Hmmm, does this mean I could just declare

name => /(?i:$Pattern->Text)/,

 
I suggest you try to find out :) However, I would suggest:
Code:
name => "(?i:".$Pattern->Text.")",
as the argument for 'name' should (probably) be a string and not a slash-delimited regex :)
 
Well, the docs say that field "can be" a delimited regex. It is usually a string.

My first couple of attempts have not worked out, I'll have to play with it at home when I can concentrate.

Thx for the help, I will post back when I have experimented more.
 
I'm getting nowhere. I've tried your string suggestion 100 ways, I've tried a regex 1000 ways, either it won't compile or it won't match.
Find::File::Rule.pod said:
Matching Rules:
name( @patterns ) Specifies names that should match. May be globs or regular expressions.

$set->name( '*.mp3', '*.ogg' ); # mp3s or oggs
$set->name( qr/\.(mp3|ogg)$/ ); # the same as a regex
$set->name( 'foo.bar' ); # just things named foo.bar
Using the 2nd example, as soon as I do m/.../ the variable becomes a reference, rather than the referent.

Follows is a (stripped down) fully functional script.
Code:
use Win32::GUI;
use File::Find::Rule;

my $main = Win32::GUI::Window->new(
	-name 	=> 'Main',
	-text 	=> 'Test',
	-width 	=> 225,
	-height => 100,
	-menu 	=> $Menu,
);
$main->Center();
 
my $Pattern = $main->AddTextfield(
        -left   => 100,
        -top    => 10,
        -prompt => "   List Files Named:",
	-height => 20,
	-width 	=> 100,
	-text 	=> "*.*",
);

my $GoButton = $main->AddButton(
	-name 	=> "GoButt",
	-size 	=> [75,25],
	-text 	=> "Start Search",
	-pos  	=> [70,35],
);

$main->Show();
Win32::GUI::Dialog();

sub GoButt_Click {
	my $pt = $Pattern->Text;
	my $depth = "1";
	my $found_files = find(
		name 	=>  "$pt",
		maxdepth => "$depth",
		exec 	=> \&show_it,
		in 	=> ("c:\\"),
	);
}

sub show_it {
	print "@_[2]\n";
}

sub Main_Terminate {
	return -1;
}

sub Exit {
	return -1;
}
I have de-referenced $Pattern->Text with
Code:
$pt=Pattern->Text;
.
.
name => $pt
in an attempt to simplify, but it hasn't helped.

How can I make $Pattern->Text case-insensitive? I'm sure it's a classic case of not seeing the forest for the trees...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top