CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
variable in regex

 



clued
stranger

Aug 17, 2001, 12:10 PM

Post #1 of 4 (6208 views)
variable in regex Can't Post

I'm totally at a loss,

In the code below, I am getting input from a form.
This input contains the string <insert>.
I then open a directory with several text files.
The text files contain 0ne line of text each.
I then put the place the contents of each text file
into the variable $line.

I want this to do a simple thing.
Insert the value of $line into the right
place in $input, and print the results.

The code below prints the value of $input, but
leaves the value of $line blank.
If I put "print "$line";" above "print "$input";"
the value of $line is printed, so I know that $line
has a value.

question is...why won't the regex do the substitution?
I can change the regex to

$input =~ s/<insert>/sometext/;

and it faithfully substitutes sometext for <insert>.
I know you can use a variable in a regex, and I'm totally
stumped. I know it's probably something simple, just a little
out of my experience.

my $input = $form{'input'};
opendir THEDIR, "$config{'path'}" or &error("packs directory could not be opened.");
my @allfiles = readdir THEDIR;
closedir THEDIR;
my $file;
foreach $file (@allfiles) {
open THEFILE, "$config{'path'}$file";
my ($line) = <THEFILE>;
close THEFILE;
chomp ($line);
$input =~ s/<insert>/$line/;
print "$input";
}
}

Thanks for any help

Bob



wickedxter
User

Aug 18, 2001, 9:40 PM

Post #2 of 4 (6199 views)
Re: variable in regex [In reply to] Can't Post

make sure that its reading the filenames to the array. And try it with out using chomp.


u might wanna use
$input =~ s/<insert>/test/g;

option g changes all occurrences of the pattern.



chengkai
stranger

Feb 23, 2002, 5:53 PM

Post #3 of 4 (6158 views)
Re: [clued] variable in regex [In reply to] Can't Post


In Reply To
I'm totally at a loss,

In the code below, I am getting input from a form.
This input contains the string <insert>.
I then open a directory with several text files.
The text files contain 0ne line of text each.
I then put the place the contents of each text file
into the variable $line.

I want this to do a simple thing.
Insert the value of $line into the right
place in $input, and print the results.

The code below prints the value of $input, but
leaves the value of $line blank.
If I put "print "$line";" above "print "$input";"
the value of $line is printed, so I know that $line
has a value.

question is...why won't the regex do the substitution?
I can change the regex to

$input =~ s/<insert>/sometext/;

and it faithfully substitutes sometext for <insert>.
I know you can use a variable in a regex, and I'm totally
stumped. I know it's probably something simple, just a little
out of my experience.

my $input = $form{'input'};
opendir THEDIR, "$config{'path'}" or &error("packs directory could not be opened.");
my @allfiles = readdir THEDIR;
closedir THEDIR;
my $file;
foreach $file (@allfiles) {
open THEFILE, "$config{'path'}$file";
my ($line) = <THEFILE>
close THEFILE;
chomp ($line);
$input =~ s/<insert>/$line/;
print "$input";
}
}

Thanks for any help

Bob



yapp
User

Feb 24, 2002, 6:23 AM

Post #4 of 4 (6150 views)
Re: [chengkai] variable in regex [In reply to] Can't Post

If you put a variable in a regexp, it's seen as a regular expression.

[perl]
my $var = "Hello there..."; # the . is a regexp meta character
my $subst = "<sometext>";

my $text = "Hello there dear user,";

$text =~ s/\Q$var/$substr/g; # /g = replace all occurences
[/perl]

To write regexp code after the \Q, use a \E, like s/\Q$var\E(...)/

Yet Another Perl Programmer

_________________________________
~~> [url=http://www.codingdomain.com]www.codingdomain.com <~~
More then 3500 X-Forum [url=http://www.codingdomain.com/cgi-perl/downloads/x-forum]Downloads! Cool

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives