A newBie Query

N

Naren

Hello grp,
I am searching for a perfect match of a string in a line and want to
replace all occurances of the string with a new string and also want a count
of the replacements

What I mean by a perfect match is that it should not be a substring of any
other string,should be independent.

$a is my string to be searched
$cnt = ($line =~ s/$a\w+/$tobereplaceed)

I dont get a correct output.

PLz help

Thaanx in advance
Rgds,
Naren.
 
T

Thens

On Wed, 17 Sep 2003 12:25:26 +0530

# Hello grp,
# I am searching for a perfect match of a string in a line and want to
# replace all occurances of the string with a new string and also want a count
# of the replacements
#
# What I mean by a perfect match is that it should not be a substring of any
# other string,should be independent.
#
# $a is my string to be searched
# $cnt = ($line =~ s/$a\w+/$tobereplaceed)

$cnt = ( $line =~ s/\b$a\b/$tobereplaced/g )
^^ ^^ ^^

\b - asserts for a word boundary
g - to repeat the search and replace globally for the string.

Also, you might run into some problems when $a has some meta
characters like +, ?, (, ). Use Quotemeta to transform them.

perldoc perlre for more details.


Regards,
Thens.
 
T

Tad McClellan

Naren said:
Subject: A newBie Query


Please put the subject of your article in the Subject of your article.

I am searching for a perfect match of a string in a line and want to
replace all occurances of the string with a new string and also want a count
of the replacements


Use s///g;

What I mean by a perfect match is that it should not be a substring of any
other string,should be independent.


The count must always be one (or zero) if it is not a substring
of any other string, as whatever sub-part you match must necessarily
be a substring of the entire string being searched.

I think you've used the wrong terminology, but I can't divine
what term you were looking for...

$a is my string to be searched


No it isn't. Your code below searches $line, not $a.

$cnt = ($line =~ s/$a\w+/$tobereplaceed) ^^
^^
I dont get a correct output.


A syntax error is never the correct output!

What output _do_ you get?

What output were you expecting to get?

Why were you expecting to get that?



We don't know what data is in $a.

We don't know what data is in $line.

We don't know what data is in $tobereplaceed.

We do not know what the input is.

We don't know what the code is.

We do not know what the output is intended to be.


Please help the helpers to help you by helpfully providing enough
information for the helpers to be _able_ to help you.

Have you seen the Posting Guidelines that are posted here frequently?



use PSI::ESP;

Maybe this is what you are looking for:

my $cnt = $line =~ s/\b$a\b/$tobereplaceed/g;

or maybe:

my $cnt = $line =~ s/\b\Q$a\E\b/$tobereplaceed/g;
 
N

Naren

Hello Grp,
Thanks for all the suggestions and advices.I could do the required
functionality.I shall specify the problems in a detailed manner in future.

Thanks again

Rgds,
Naren.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top