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

Re: [PBML] Re: Regular Expression Question: Is this syntax legal?

Expand Messages
  • Marcos Ramos
    /^$thing/ Isn t that because you have $ (end line regex) just after ^ (line start reged) ? So it tries to match empty line followed by the word thing . --
    Message 1 of 10 , Aug 7, 2009
    • 0 Attachment
      "/^$thing/"

      Isn't that because you have $ (end line regex) just after ^ (line
      start reged) ?
      So it tries to match empty line followed by the word "thing".

      --
      Marcos Ramos
      e-mail/google-talk/msn-messenger: marcos.ramos@...
      skype/twitter: marcosjsramos

      No dia 2009/08/06, às 21:41, MAJID AGHA AMINI <amini4@...>
      escreveu:

      > #!C:\Perl\bin\perl.exe
      > print "enter your ...","\n";
      > $majid=<STDIN>;
      > chomp($majid);
      > $_="[test]";
      > if($majid eq $_) {
      > print "yah","\n";
      > }
      > else {
      > print "boooo...\n";
      > }
      >
      > --- On Tue, 7/28/09, call_ashutosh2003 <ashutosh.call@...>
      > wrote:
      >
      > From: call_ashutosh2003 <ashutosh.call@...>
      > Subject: [PBML] Re: Regular Expression Question: Is this syntax legal?
      > To: perl-beginner@yahoogroups.com
      > Date: Tuesday, July 28, 2009, 7:31 PM
      >
      >
      >
      > Why are you putting ^ in "/^$thing/" when you assigne $_ to "[test]"
      > write (/$thing/) as $_ now begins with [ ;)
      >
      > --- In perl-beginner@ yahoogroups. com, "fooguy89" <fooguy89@.. .>
      > wrote:
      > >
      > > Let's say I have this snippet:
      > >
      > > $_ = "test";
      > > my $thing = "test";
      > >
      > > if (/^$thing/)
      > > {
      > > print "Yah!\n";
      > > }
      > > else
      > > {
      > > print "booo...\n";
      > > }
      > >
      > > It correctly prints "Yah!". If I change $_ to "xxx" it correctly
      > prints "booo...".
      > >
      > > However, I'm a bit stumped as to what to do if my string to test
      > has [ and ] characters, i.e.
      > >
      > > $_="[test]";
      > > my $thing = "[test]";
      > >
      > > This incorrectly prints "booo...". I realize that [ and ] are
      > special regex characters. I tried using "\[test\]" in both $thing
      > and $_, but it still fails.
      > >
      > > If this is indeed valid syntax (having a variable inside a regex),
      > how would I get it to work?
      > >
      > > I realize there are probably better ways to do this, but at this
      > point it's become a thorn in my side to find the answer!
      > >
      > > Thanks.
      > >
      >
      > [Non-text portions of this message have been removed]
      >
      >


      [Non-text portions of this message have been removed]
    • Jenda Krynicky
      From: Marcos Ramos ... No. In this case Perl interpolates the variable into the regexp and uses the contents as part of the
      Message 2 of 10 , Aug 10, 2009
      • 0 Attachment
        From: Marcos Ramos <marcos.ramos@...>
        > "/^$thing/"
        >
        > Isn't that because you have $ (end line regex) just after ^ (line
        > start reged) ?
        > So it tries to match empty line followed by the word "thing".

        No. In this case Perl interpolates the variable into the regexp and
        uses the contents as part of the regexp. The thing is that if there
        are any characters special for regexps they are treated specially. So
        if $thing contains '[test]', then the regexp checked will be
        /^[test]/. Which means ... find one of the characters "t", "e" or "s"
        at the beginning of the matched string.

        Jenda
        ===== Jenda@... === http://Jenda.Krynicky.cz =====
        When it comes to wine, women and song, wizards are allowed
        to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery
      Your message has been successfully submitted and would be delivered to recipients shortly.