Quick Regex Subst question

Discussion in 'Perl Misc' started by Marv, Dec 28, 2003.

  1. Marv

    Marv Guest

    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
     
    Marv, Dec 28, 2003
    #1
    1. Advertising

  2. Marv

    Ben Morrow Guest

    Marv <> 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 #
    $J::u::s::t, $a::n::eek:::t::h::e::r, $P::e::r::l, $h::a::c::k::e::r, $.
     
    Ben Morrow, Dec 28, 2003
    #2
    1. Advertising

  3. Marv

    Eric Amick Guest

    On Sun, 28 Dec 2003 19:16:53 GMT, Marv <> 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
     
    Eric Amick, Dec 28, 2003
    #3
    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. subst and IIS on XP

    , Jan 23, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,466
  2. Chris
    Replies:
    1
    Views:
    602
    Chad Z. Hower aka Kudzu
    Feb 17, 2004
  3. JKop
    Replies:
    11
    Views:
    904
  4. gamename

    Equivalent of TCL's "subst" ?

    gamename, Nov 13, 2007, in forum: Python
    Replies:
    4
    Views:
    605
    gamename
    Nov 17, 2007
  5. Replies:
    3
    Views:
    781
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page