Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Quick Regex Subst question (http://www.velocityreviews.com/forums/t884368-quick-regex-subst-question.html)

Marv 12-28-2003 07:16 PM

Quick Regex Subst question
 
Hello,

I'm new to regex and hoping someone can give me a quick answer to this
question as I can't seem to find it anywhere.

I'm trying to parse a line and subsitute any one charactor followed by
a $ and surrounded by a front slash on both sides with the same
charactor followed by a space and "(Drive)". This also must occur at
the beginning of the line. So basically substitute "$" for "(Drive)".

Ex.

/C$/path/path/

ends up as:

/C (Drive)/path/path

I've got the following so far:

$test = '/C$/path/path/';

$test =~ s/^\/.\$/\/.\(Drive\)/;

But when I do: printf "$test\n";

I get:

/.(Drive)/account/test/

I know the dot is wrong, but how do I carry the "C" over in the
substituion.

Thanks in advance.

Marv

Ben Morrow 12-28-2003 07:52 PM

Re: Quick Regex Subst question
 

Marv <marv@mister.com> wrote:
> Hello,
>
> ends up as:
>
> /C (Drive)/path/path
>
> I've got the following so far:
>
> $test = '/C$/path/path/';
>
> $test =~ s/^\/.\$/\/.\(Drive\)/;


Bleech! Use some other delimiter to avoid all those backwhacks. Use /x
to clean things up a bit. The second half of an s/// is not a regex,
just a double-quotey string, so () don't need escaping.

$test =~ s|^/ (.) \$ /|/$1 (Drive)/|x;

See perldoc perlretut "Extracting matches" for how the () abd the $1
join up.

I would be tempted to use look{ahead,behind} here, just because it
feels cleaner; viz.:

$test =~ s|(?<= ^/) (.) \$ (?= /)|$1 (Drive)|x;

but it's probably too complex to be worth bothering with here.

> But when I do: printf "$test\n";


Don't use printf when you men print. I don't think I have ever used
printf in perl... even when it would be useful, I instinctively use
'print sprintf ...' instead :).

Ben

--
$.=1;*g=sub{print@_};sub r($$\$){my($w,$x,$y)=@_;for(keys%$x){/main/&&next;*p=$
$x{$_};/(\w)::$/&&(r($w.$1,$x.$_,$y),next);$y eq\$p&&&g("$w$_")}};sub t{for(@_)
{$f&&($_||&g(" "));$f=1;r"","::",$_;$_&&&g(chr(0012))}};t # ben@morrow.me.uk
$J::u::s::t, $a::n::o::t::h::e::r, $P::e::r::l, $h::a::c::k::e::r, $.

Eric Amick 12-28-2003 09:40 PM

Re: Quick Regex Subst question
 
On Sun, 28 Dec 2003 19:16:53 GMT, Marv <marv@mister.com> wrote:

>I'm trying to parse a line and subsitute any one charactor followed by
>a $ and surrounded by a front slash on both sides with the same
>charactor followed by a space and "(Drive)". This also must occur at
>the beginning of the line. So basically substitute "$" for "(Drive)".
>
>Ex.
>
>/C$/path/path/
>
>ends up as:
>
>/C (Drive)/path/path
>
>I've got the following so far:
>
>$test = '/C$/path/path/';
>
>$test =~ s/^\/.\$/\/.\(Drive\)/;


If you surround a portion of a regex with parentheses, you can refer to
the text it matches with $1 later.

$test =~ s!^/(.)\$/!/$1 (Drive)/!;

Note that you can use just about any character to surround the regex and
substitution string, which makes working with slashes a lot less
painful.

See perldoc perlretut, particularly the section called "Extracting
Matches".

--
Eric Amick
Columbia, MD


All times are GMT. The time now is 07:58 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.