Loading ...
Sorry, an error occurred while loading the content.

[PBML] Re: substitution

Expand Messages
  • jonathansamuel@yahoo.com
    Upon rereading the statement in the Camel book, I now see that 1, 2 etc. is only for use inside of the regular expression itself. Your $1, $2 values are not
    Message 1 of 6 , Aug 2, 2001
    • 0 Attachment
      Upon rereading the statement in the Camel book, I now see that \1, \2
      etc. is only for use inside of the regular expression itself. Your
      $1, $2 values are not actually within the regular expression itself.
      Rather, they are within what Camel calls "the replacement part of the
      substition."

      Thus you were right all along to be using $1, $2 etc.




      --- In perl-beginner@y..., "Shepard, Gregory R" <gregory.r.shepard@x>
      wrote:
      > Your advise on using \1 \2 etc as oppose to $1 $2 etc is accurate
      from what
      > I read. I did try that in the beginning and it did work, however, I
      received
      > some output messages stating I should use the $1 instead of the \1 -
      so I
      > uses the $1.
      >
      >
      >
      > -----Original Message-----
      > From: jonathansamuel@y... [mailto:jonathansamuel@y...]
      > Sent: Thursday, August 02, 2001 10:47 AM
      > To: perl-beginner@y...
      > Subject: [PBML] Re: substitution
      >
      >
      > It certainly seems as if $1, $2, etc. in your regular expression is
      a
      > valid backreference as far as Perl is concerned.
      >
      > That is odd, because it says on page 41 of the third edition of
      > Programming Perl (the Camel Book) that: "How you refer back to the
      > remembered part of the string depends on where you want to do it
      > from. Within the same regular expression, you use a backslash
      > followed by an integer...Outside the regular expression itself,
      such
      > as in the replacement part of a substitution, you use a $ followed
      by
      > an integer."
      >
      > Your solution appears to violate the above rule and uses $1, $2
      etc.
      > inside the regex, but it works perfectly.
      >
      > Also, I now see that $n is a Perl variable that the regex
      > interpolates. Your use of it is straightforward.
      >
      >
      >
      >
      > --- In perl-beginner@y..., "Shepard, Gregory R"
      <gregory.r.shepard@x>
      > wrote:
      > > Let me give you the output before and after the regex...
      > > Code:
      > > $cur_time = localtime();
      > > $xtime = $cur_time;
      > > $print "preformatted time: $xtime\n";
      > > output-> Thu Aug 2 15:13:33 2001
      > > $n='_';
      > > $xtime =~
      > > s/(\w{3}).*(\w{3}).*(\d\d?).*(\d\d?).*(\d\d).*/
      > $n.$1.$n.$2.$n.$3.$n.$4.$n.$5
      > > /e;
      > > print "reformatted time: $xtime\n";
      > > output-> _Thu_Aug_2_0_01
      > >
      > > Later in my script, I created a file using a process name
      > concatenated to
      > > the reformated date concatenated to ".log", thus giving me the
      file
      > name:
      > > <name of process>_Thu_Aug_2_0_01.log
      > > I needed the underscore to make it a valid file since perl can
      not
      > work with
      > > files that have spaces in them.
      > >
      > > Thanks for all you help guys...
      > >
      > > Greg
      > >
      > >
      > > -----Original Message-----
      > > From: jonathansamuel@y... [mailto:jonathansamuel@y...]
      > > Sent: Thursday, August 02, 2001 9:56 AM
      > > To: perl-beginner@y...
      > > Subject: [PBML] Re: substitution
      > >
      > >
      > > --- In perl-beginner@y..., "Shepard, Gregory R"
      > <gregory.r.shepard@x>
      > > wrote:
      > > > I must admit, I found the answer to my own question. I needed
      to
      > at
      > > * to
      > > > capture the empty spaces in-between between the regex's.
      > > > bad regex =
      > > > s/(\w{3}).(\w{3}).(\d\d?).(\d\d?).(\d\d).*/
      > > $n.$1.$n.$2.$n.$3.$n.$4.$n.$5/e;
      > > > good regex =
      > > > s/(\w{3}).*(\w{3}).*(\d\d?).*(\d\d?).*(\d\d).*/
      > > $n.$1.$n.$2.$n.$3.$n.$4.$n.$5
      > > > /e;
      > > > I should've gave the output of the $xtime to supply more info.
      > > Also, $n was
      > > > needed to attach the characters in the reformatted regex so I
      > could
      > > use it
      > > > as a file.
      > >
      > >
      > > I don't get it. What does '$n' do exactly? is it a literal?
      > >
      > > Mastering Regular Expressions by Jeffrey Friedl doesn't say
      > anything
      > > at all about a special variable called '$n'.
      > >
      > > Also are $1, $2 etc. supposed to be the results captured by your
      > > parentheses? Because that ought not to work either. Inside of the
      > > regex one is supposed to use \1, \2 etc.
      > >
      > >
      > >
      > >
      > >
      > >
      > >
      > >
      > > Your use of Yahoo! Groups is subject to
      > http://docs.yahoo.com/info/terms/
      >
      >
      >
      >
      >
      > Your use of Yahoo! Groups is subject to
      http://docs.yahoo.com/info/terms/
    Your message has been successfully submitted and would be delivered to recipients shortly.