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

troubles in matching +-*/ using regrex

Expand Messages
  • ssysong
    i used the string (+)|(-)|(*)|( )|(/) to match the mathmatic symbols.but it did not work.below is the code.
    Message 1 of 2 , May 16, 2013
    View Source
    • 0 Attachment
      i used the string '(+)|(-)|(*)|(\\)|(/)' to match the mathmatic symbols.but it did not work.below is the code.
      ____________________________________________________________________________

      #! /usr/bin/perl -w
      use strict;
      my $op_pattern = '(+)|(-)|(*)|(\\)|(/)';
      my $f = '-1+-2/3\4';
      if($f =~ m/$op_pattern/){
      print "ok","\n";
      }else{
      print "NO","\n";
      }

      [Non-text portions of this message have been removed]
    • timothy adigun
      Hi ssysong, ... write this my $op_pattern = qr[( +|-| *| |/)]; instead. Please don t forget that both + and * have meaning in regex. ... note that this
      Message 2 of 2 , May 16, 2013
      View Source
      • 0 Attachment
        Hi ssysong,

        On Fri, May 17, 2013 at 4:05 AM, ssysong <ubuntubiker@...> wrote:

        > **
        >
        >
        > i used the string '(+)|(-)|(*)|(\\)|(/)' to match the mathmatic
        > symbols.but it did not work.below is the code.
        > __________________________________________________________
        >
        > #! /usr/bin/perl -w
        > use strict;
        > my $op_pattern = '(+)|(-)|(*)|(\\)|(/)';
        >
        write this
        my $op_pattern = qr[(\+|-|\*|\\|/)];
        instead. Please don't forget that both '+' and '*' have meaning in regex.

        > my $f = '-1+-2/3\4';
        > if($f =~ m/$op_pattern/){
        >
        note that this match would be successful at the '-' before number 1, then
        you have 'OK' been printed

        > print "ok","\n";
        > }else{
        > print "NO","\n";
        > }
        >
        so your code is:
        [CODE]

        #!/usr/bin/perl -w
        use strict;

        my $op_pattern = qr[(\+|-|\*|\\|/)];
        my $f = '-1+-2/3\4';

        if ( $f =~ m/$op_pattern/ ) {
        print "ok", "\n";
        }
        else {
        print "NO", "\n";
        }
        [/CODE]

        Hope that helps.

        > [Non-text portions of this message have been removed]
        >
        >
        >



        --
        Tim


        [Non-text portions of this message have been removed]
      Your message has been successfully submitted and would be delivered to recipients shortly.