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';
| [reply] [d/l] |
Re: Regex doubt
by tulsyan (Beadle) on Jul 29, 2005 at 10:38 UTC
|
$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
| [reply] [d/l] [select] |
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/;
| [reply] [d/l] [select] |
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
;-)
| [reply] [d/l] |
|
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.
| [reply] |
|
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.
| [reply] |
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!
| [reply] |
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.
| [reply] [d/l] |
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/;
| [reply] [d/l] |
|
| [reply] |
|
I mean exactly \Q \E quoting in regex.
| [reply] [d/l] |
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 | [reply] [d/l] |
|
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
| [reply] [d/l] |
|
AFAIK there's no special \W escape sequence. Thus $line boils down to "this is TelecommuterTracWebIntHitList3". HTH
| [reply] [d/l] [select] |
A reply falls below the community's threshold of quality. You may see it by logging in. |