A newBie Query

Discussion in 'Perl Misc' started by Naren, Sep 17, 2003.

  1. Naren

    Naren Guest

    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.
     
    Naren, Sep 17, 2003
    #1
    1. Advertising

  2. Naren

    Thens Guest

    On Wed, 17 Sep 2003 12:25:26 +0530
    "Naren" <> wrote:

    # 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.
     
    Thens, Sep 17, 2003
    #2
    1. Advertising

  3. Naren <> wrote:

    > 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?


    > PLz help



    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;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Sep 17, 2003
    #3
  4. Naren

    Naren Guest

    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.
     
    Naren, Sep 18, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Learner
    Replies:
    1
    Views:
    1,027
    Marina Levit [MVP]
    Jan 30, 2006
  2. Anonymous
    Replies:
    0
    Views:
    1,550
    Anonymous
    Oct 13, 2005
  3. David Gordon

    xpath query query

    David Gordon, May 18, 2005, in forum: XML
    Replies:
    2
    Views:
    833
    David Gordon
    May 18, 2005
  4. Eric Nelson
    Replies:
    5
    Views:
    1,579
    Alexey Smirnov
    Feb 4, 2009
  5. Jon F.

    CAML Query: Multiple Query Fields Issue

    Jon F., May 12, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    774
    Jon F.
    May 12, 2004
Loading...

Share This Page