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

iterpolating matched string replacement instead of search string 1

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hi,

I've been thinking about my search facility and decided to add some matched text highlighting.

However, i'm currently using the search term as the replacement text, and as I am doing patrial 'LIKE' match, the text may be in the middle of a word, as I am using CSS to transform the font to capitalize, it's making the highlighting look odd when the replacement is done.

ok I could remove the CSS, but I still have the problem currenly if I simply use the search text and someone types as their search
DaNcE MuSiC
it replaces the text with the upper and lower case as it's typed.

so the question...

How do I do a substitute interpolating the search term and required replacement markup , but it use the text found in the string to keep its character set intact.

current code...
Code:
# get albums
my @albums = &getSQL("Albums","*","$sql","Rec_ID DESC");

my $i = @albums+1;
[b]my $replace = "<span class=\"red\">$search</span>";[/b]
#loop album and build track listing
for(@albums){
    $i--;
    my @tracks = &getSQL("Tracks","*","Album = '$_->{'Album_Title'}' AND Artist = '$_->{'Artist'}'","Track_No ASC");
    foreach my $trk(@tracks){
        $trk->{'Disp_Title'} = $trk->{'Track_Title'};
        [b]if($search ne ""){  
            $trk->{'Disp_Title'} =~ s/$search/$replace/gi
        }[/b]
    }        
    $_->{'Tracks'} = \@tracks;
    $_->{'CNT'} = $i;
    $_->{'Disp_Title'} = $_->{'Album_Title'};
    $_->{'Disp_Artist'} = $_->{'Artist'}; 
    $_->{'Disp_Genre'} = $_->{'Genre_Desc'};     
    [b]if($search ne ""){                 
        $_->{'Disp_Title'} =~ s/$search/$replace/gi;
        $_->{'Disp_Artist'} =~ s/$search/$replace/gi;  
        $_->{'Disp_Genre'} =~ s/$search/$replace/gi;               
    }[/b]
    
}

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
I'm trying to get my head round this Extracting matches, but it doesn't seem to work.
Code:
my $replace = "<span class=\"red\">$1</span>";
$trk->{'Disp_Title'} =~ s/($search)/$replace/i;

But it seems to replace it with blank, so $1 must be empty?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Hard code it in the regexp:


Code:
$trk->{'Disp_Title'} =~ s/($search)/<span class="red">$replace</span>/i;

otherwise you get into needing to use the "e" modifier, or "ee" actually in this case, to eval (twice) the replacement side of the regular expression.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I'm still confused, should that be
Code:
$trk->{'Disp_Title'} =~ s/($search)/<span class="red">[b]$1[/b]</span>/i;

I have been trying with...
Code:
$_->{'Disp_Genre'} =~ s/($search)/$replace/gie; 

AND

$_->{'Disp_Genre'} =~ s/($search)/$replace/giee;
but they didn't work either

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Yea, my bad, use $1:

Code:
$trk->{'Disp_Title'} =~ s/($search)/<span class="red">$1</span>/i;

This looks like it should work:

Code:
$_->{'Disp_Genre'} =~ s/($search)/$replace/giee;






------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
lol - just a little wteek and it worked...
Code:
            $trk->{'Disp_Title'} =~ s/($search)/<span class="red">$1<[b]\[/b]/span>/gi;
needed to escape the forward slash in the end tag.

If my logic was not flawed, why did it not work when I used e(evaluate) and even double e when using the $replace var which included the $1 var for evaluation?

hmm, just when you think you've learned something and try to apply it, something always throws a spanner in the works.

Just as well there's Tek-Tips :)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
OK, upon closer look I see the problem. You used double-quotes to define the value of $replacement:

Code:
my $replace = "<span class=\"red\">$1</span>";


What happens in double-quoted strings? Variable interpolation! So $1 is interpolated (but it is undefined). So now the literal value of $replacement is:

Code:
<span class="red"></span>

Are you following me?

What you need to do is define the left-side of $replacement with single-quotes so there is no interpolation:

Code:
my $replace = '<span class="red">$1</span>';

So now the literal value of $replacment is:

Code:
<span class="red">$1</span>


No variable interpolation in a single-quoted construct so $1 is still in the string, all be it as a literal string and not a scalar variable. But now if you use it in the regexp and use "ee" perl will evaluate it turn it into the code you want:

Code:
# get albums
my @albums = &getSQL("Albums","*","$sql","Rec_ID DESC");

my $i = @albums+1;
[red]my $replace = '<span class="red">$1<\/span>';[/red]
#loop album and build track listing
for(@albums){
    $i--;
    my @tracks = &getSQL("Tracks","*","Album = '$_->{'Album_Title'}' AND Artist = '$_->{'Artist'}'","Track_No ASC");
    foreach my $trk(@tracks){
        $trk->{'Disp_Title'} = $trk->{'Track_Title'};
        if($search ne ""){  
            $trk->{'Disp_Title'} =~ s/$search/$replace/gi[red]ee[/red];
        }
    }        
    $_->{'Tracks'} = \@tracks;
    $_->{'CNT'} = $i;
    $_->{'Disp_Title'} = $_->{'Album_Title'};
    $_->{'Disp_Artist'} = $_->{'Artist'}; 
    $_->{'Disp_Genre'} = $_->{'Genre_Desc'};     
    if($search ne ""){                 
        $_->{'Disp_Title'} =~ s/$search/$replace/gi[red]ee[/red];
        $_->{'Disp_Artist'} =~ s/$search/$replace/gi[red]ee[/red];
        $_->{'Disp_Genre'} =~ s/$search/$replace/gi[red]ee[/red];
    }
}

Hopefully I got all the correct.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
What happens in double-quoted strings? Variable interpolation! So $1 is interpolated (but it is undefined). So now the literal value of $replacement is:


Code:
<span class="red"></span>
Are you following me?

What you need to do is define the left-side of $replacement with single-quotes so there is no interpolation:

Yes of course, I'm still with you ;-)

$1 was already interpolated due to double quoted string, do'h.

But i'm still pretty pleased with my effort, it was only from the other thread 2 days ago i'd learnt what variable interpolation via a regex subsitution was all about :)

Code:
my $replace = '<span class="red">$1<\/span>';
I noticed you have escaped the forward slash in the $replace variable ? is that necessary, isn't it only necessary if you hard code the html string in the regex itself?





"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
I noticed you have escaped the forward slash in the $replace variable ? is that necessary, isn't it only necessary if you hard code the html string in the regex itself?

No its not necessary, not sure why I stuck that in, just instinct maybe. Here is the code that works:

Code:
my $replace = '"<span class=\"red\">$1</span>"';
my $string = 'this is a test of the code';
$string =~ s/(test)/$replace/ee;
print $string;

Note the double-quotes in $replace.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
No its not necessary, not sure why I stuck that in, just instinct maybe.
your instinct must have let you down yesterday as you forgot it in the hard coded version (which i'm currently using), and my program bombed [lol]

Code:
my $replace = '"<span class=\"red\">$1</span>"';
Why the single & double quotes in the $replace ?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
I'm not always the best at technical explantions but.....


The first and last double-quotes are there so the first "e" in "ee" evals the string as a double-quoted construct (so it can be subject to variable interpolated) and the second "e" is there so $1 is interpolated as a matching back reference variable to capture whats in the parenthisis on the search side of the regexp. Try it with one "e" and see what I mean.

If I got that wrong hopefully someone will correct me. You can probably also look it up in perlretut.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
So the first single quotes to stop initial $1 variable interpolation being done when creating $replace.

Then the double quotes to force evaluation of any variable interpolation required in the string when executed, which allows the use of $1 for back reference capture.

Is that what you said, but in my language :lol:



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
So the first single quotes to stop initial $1 variable interpolation being done when creating $replace.

Then the double quotes to force evaluation of any variable interpolation required in the string when executed, which allows the use of $1 for back reference capture.

Is that what you said, but in my language [lol]



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Is that what you said, but in my language

pra-koo nekhoe setor raptus kaloina [smile]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I'd love to know what you said but all Google Translate says is
We are not yet able to translate from Malay into English.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Its blabber, doesn't mean anything. [reading]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Well Google thinks it's Malyan, so what's that telling you?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Its telling me: afubarn kleptor reptis noc-too opso.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Man 'Google Translate' sucks!

Translation: Czech (automatically detected) » English
afubarn kleptor reptis noc-too opso = afubarn kleptor reptis night-too opso


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top