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

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

Expand Messages
  • sudha karan
    You can try as below. my $_ = [test] ; my $thing = [test] ; $thing =~ s/ [/ [/g; if (/^$thing/) { print Yah! n ; } else { print booo... n ; } Sudhakaran.
    Message 1 of 10 , Jul 28, 2009
    • 0 Attachment
      You can try as below.

      my $_ = "[test]";
      my $thing = "[test]";
      $thing =~ s/\[/\\\[/g;
      if (/^$thing/)
      {
      print "Yah!\n";
      }
      else
      {
      print "booo...\n";
      }

      Sudhakaran.

      --- On Tue, 28/7/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, 28 July, 2009, 8: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.

      >





























      Looking for local information? Find it on Yahoo! Local http://in.local.yahoo.com/

      [Non-text portions of this message have been removed]
    • merlyn@stonehenge.com
      ... sudha You can try as below. But Q handles a lot more than [. And should be the preferred solution. -- Randal L. Schwartz - Stonehenge Consulting
      Message 2 of 10 , Jul 28, 2009
      • 0 Attachment
        >>>>> "sudha" == sudha karan <sudhagkaran@...> writes:

        sudha> You can try as below.

        But \Q handles a lot more than [.

        And should be the preferred solution.

        --
        Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
        <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
        Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
        See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
      • fooguy89
        The Q ... E as suggested works great. Thanks all!
        Message 3 of 10 , Jul 28, 2009
        • 0 Attachment
          The \Q ... \E as suggested works great.

          Thanks all!


          --- In perl-beginner@yahoogroups.com, merlyn@... wrote:
          >
          > >>>>> "sudha" == sudha karan <sudhagkaran@...> writes:
          >
          > sudha> You can try as below.
          >
          > But \Q handles a lot more than [.
          >
          > And should be the preferred solution.
          >
          > --
          > Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
          > <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
          > Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
          > See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
          >
        • MAJID AGHA AMINI
          #!C: Perl bin perl.exe print enter your ... , n ; $majid= ; chomp($majid); $_= [test] ; if($majid eq $_) { print yah , n ; } else { print
          Message 4 of 10 , Aug 6, 2009
          • 0 Attachment
            #!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]
          • 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 5 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 6 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.