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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reg expresion problem

Status
Not open for further replies.

dwhalen

Programmer
Feb 22, 2002
105
CA
Hey,

I am trying to do a regular expression but I am running into a problem because the scalar that I am doing the test on may or may not have regular expression syntax in it. When it has regular expression syntax in it...it doesn't work...confused? Maybe an example will explain it better than me :)

Code:
#options is an array that contains select box options:

my @options = ("<option value=\"apg_status iLIKE 'Active' OR apg_status = '2004'&Active|2004\">2004</option>",
               "<option value=\"apg_status iLIKE 'Active' OR apg_status = '2005'&Active|2005\">2005</option>",
                "<option value=\"apg_status iLIKE 'Active' OR apg_status = '2006'&Active|2006\">2006</option>",
                "<option value=\"apg_status iLIKE 'Active' OR apg_status = '2006'&Active\">2006</option>"
                );
#as you can see some of the options have &Active|2006 at the
#end...it is these ones that are causing the problem

#status is what I am testing for:

#this doesn't work
my $status = "apg_status iLIKE 'Active' OR apg_status = '2006'&Active|2006";

#this does work
#my $status = "apg_status iLIKE 'Active' OR apg_status = '2006'&Active";


map{$_=~s/<option value=(["']$status['"])>/<option value=$1 selected>/gi}@options;

print("@options\n");

I think it is the | in the value that is causing the problem (I could be wrong). How can I get the regular expression to ignore any of this syntax in the scalar?

Thanks for the help.

Later
 
Try escaping the pipe symbol when you're writing it out.

The | will to my understanding be interpolated when within quotes

"Active\|2006"

HTH
--Paul
 
Thanks for your response.

I was hoping to not have to do that. The reason being is that I use everything after & in another section of my code in a regular expression.....I just realized that I want it both ways at the same time :).

But I was wondering if there was a modifier or some way of not getting it to interpret regex syntax in a scalar in some cases and to explicity interpret them in other cases....??? Or is there no way of doing this?

Thanks for the help.


Later
 
'Active|2006' single quotes won't interpolate AFAIK

HTH
--Paul
 
my @options = ("<option value=\"apg_status iLIKE 'Active' OR apg_status = '2004'&Active|2004\">2004</option>",
               "<option value=\"apg_status iLIKE 'Active' OR apg_status = '2005'&Active|2005\">2005</option>",
               "<option value=\"apg_status iLIKE 'Active' OR apg_status = '2006'&Active|2006\">2006</option>",
               "<option value=\"apg_status iLIKE 'Active' OR apg_status = '2006'&Active\">2006</option>");

print join ("\n", @options);
print "\n\n";

map{$_=~s/(<[^>]+)">/$1 selected">/gi}@options;

print join ("\n", @options);


Kind Regards
Duncan
 
Thanks for your reply.

That sets all of the options to selected....I want only the ones that are equal to status to be set to selected.

Later
 
Hey I think I may have solved my problem...not the way I wanted to do it...but hey it works.

This is going back to what Paul said

Try escaping the pipe symbol when you're writing it out.

The | will to my understanding be interpolated when within quotes

"Active\|2006"

The thing was, I didn't want to set the | symbol to \| in my select box because I use it elsewhere. So in my map I just set a new variable to with the pipe backslashed. This may same weird...but I am not showing the rest of my program, for obvious reasons, otherwise it would make sense :).

Code:
map{    my $test = $status;
                $test =~s/\|/\\\|/g;
   $_=~s/<option value=(["']$test['"])>/<option value=$1 selected>/gi;

                }@options;

Thanks for all the help guys. I really apreciate it.

Later
 
sorry.. this works as needed without needing to escape the pipe in the strings

[red]my @options = ("<option value=\"apg_status iLIKE 'Active' OR apg_status = '2004'&Active|2004\">2004</option>",
               "<option value=\"apg_status iLIKE 'Active' OR apg_status = '2005'&Active|2005\">2005</option>",
               "<option value=\"apg_status iLIKE 'Active' OR apg_status = '2006'&Active|2006\">2006</option>",
               "<option value=\"apg_status iLIKE 'Active' OR apg_status = '2006'&Active\">2006</option>");

print join ("\n", @options);
print "\n\n";

my $status = "apg_status iLIKE 'Active' OR apg_status = '2006'&Active|2006";
#my $status = "apg_status iLIKE 'Active' OR apg_status = '2006'&Active";

print length($status)."\n\n";

foreach (@options) {
if ($status =~ /\|/) {
s/(&Active\|20\d{2})(">)/$1 selected$2/;
} else {
s/(&Active)(">)/$1 selected$2/;
}
}

print join ("\n", @options);[/red]


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top