Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Regex doubt

by kprasanna_79 (Hermit)
on Jul 29, 2005 at 07:43 UTC ( [id://479266]=perlquestion: print w/replies, xml ) Need Help??

kprasanna_79 has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Regex doubt
by borisz (Canon) on Jul 29, 2005 at 07:48 UTC
    I do not understand the question. Propably you want change $line to country??
    $line = 'country';
    Boris
Re: Regex doubt
by tulsyan (Beadle) on Jul 29, 2005 at 10:38 UTC

    Hi Prasanna.k

    Use Single Quote in $line string

    $line = 'this is TelecommuterTrac\WebIntHitList3'; $line=~s#\QTelecommuterTrac\WebIntHitList3\E#country#;

    Or

    $line = 'this is TelecommuterTrac\WebIntHitList3'; $findstr='TelecommuterTrac\WebIntHitList3'; $findstr=quotemeta($findstr); $line=~s#$findstr#country#;

    Output will get:

    this is country
Re: Regex doubt
by blazar (Canon) on Jul 29, 2005 at 09:13 UTC
    Greeting Monks, I need a solution for this problem..
    $line = "TelecommuterTrac\WebIntHitList3";
    And the problem is? I only see a line of code consisting of the assignment of a string to the variable $line
    I want to change this to "country".
    I think none of us has the slightest idea about what the pronoun "this" refers to.
    I tried in many ways of subtitution.
    You tried "in many ways of subtitution" to what?!?

    Some code would shed more light on what you want us to shed some light on, e.g.:

    $line =~ s/whatever/country/;
Re: Regex doubt
by aukjan (Friar) on Jul 29, 2005 at 07:54 UTC
    $line =~ s/TelecommuterTrac(\\)?WebIntHitList3/country/;


    Update: Do substitution on both single and double quoted lines
    ;-)
      That will leave $line unchanged, as it will change a string consisting of TelecommuterTrac followed by a backslash, followed by WebIntHitList3 to country. But $line was assigned "TelecommuterTrac\WebIntHitList3" which does not contain a backslash. It's in double quoted context, and the backslash is escaping the W from its special meaning (which it doesn't have). So, $line equals TelecommuterTracWebIntHitList3.
        That depends - the OP may have originally been presented with a problem in which the backslash was there but is losing the backslash during the testing process to try to fix the (wrong) problem.

        One world, one people

Re: Regex doubt
by anonymized user 468275 (Curate) on Jul 29, 2005 at 08:01 UTC
    To explain the above solution - the \W is the regexp for 'nonalphanumeric character' whereas you presumably want to tell it literal \ and literal W. So the '\' is being "escaped" by a backslash to load it correctly into the regexp engine.

    Update: I am explaining the solution, but that doesn't mean I am certain it's the right solution, although, as I later replied below, it is more the problem than the solution I am doubting!

    One world, one people

Re: Regex doubt
by anonymized user 468275 (Curate) on Jul 29, 2005 at 08:51 UTC
    The solution that will substitute $line the way it is assigned in your example would be:
    $line =~ s/\Q$line/country/;
    and as your code is precisely written you could even drop the \Q! But many responders are (quite reasonably in my opinion) doubting that this is the solution you want. Such respondents might reasonably think that you mean to say you have a string that does contain the backslash just before you want to substitute it - "if the backslash weren't there you wouldn't have a problem in the first place," they might conclude.

    It appears to me that we need convincing one way or another whether the real problem string you are presented with has a backslash in it or not.

    One world, one people

Re: Regex doubt
by sh1tn (Priest) on Jul 29, 2005 at 08:18 UTC
    You may want to quote the searched string on order not to bother with eascape chars:
    $line = "TelecommuterTrac\WebIntHitList3"; # the whole text which contains the string: $text =~ s/\Q$line\E/country/;


      I think you meant to put single quotes in that first line.
      ---
      my name's not Keith, and I'm not reasonable.
        I mean exactly \Q \E quoting in regex.


Re: Regex doubt
by kprasanna_79 (Hermit) on Jul 29, 2005 at 09:09 UTC
    Sorry For the choas,
    I have a line with the string "TelecommuterTrac\WebIntHitList3" and using regular expression i want it to subtitute "country" into that line. I tried many ways what all the monks said..but ended up in vein. So can any one give the exact working code...See below for more info
    $line = "this is TelecommuterTrac\WebIntHitList3"; I want it to be "this is country"
    I understood the problem..i need solution for it..it would be better if i get the working code...
    -Prasanna.K
      Your problem is, \W is a unknown escape sequence, so use ' not ".
      $line = 'this is TelecommuterTrac\WebIntHitList3'; $line =~ s/\QTelecommuterTrac\WebIntHitList3/country/; print $line __OUTPUT__ this is country
      Boris
      AFAIK there's no special \W escape sequence. Thus $line boils down to "this is TelecommuterTracWebIntHitList3". HTH
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://479266]
Approved by monkfan
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-16 22:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found