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

Usage of regular expression?

Expand Messages
  • Jeff Shu
    Hello everyone: Hello I am doing a exercise which requires me to find out all the words that contains five vowels(a, e, I, o, u) from a text file. I wrote a
    Message 1 of 10 , Dec 2, 2004
    • 0 Attachment
      Hello everyone:

      Hello

      I am doing a exercise which requires me to find out all the words
      that contains five vowels(a, e, I, o, u) from a text file.

      I wrote a the following code to test it.
      While(<>)
      {
      print if /aeiou/
      }

      but the result didn't show any words that contains five vowels. I
      am
      wondering what's wrong the my regular expression. Please give me
      some
      suggestions. Thanks!
      Jeff
      12-02-04
    • eman
      ... Your code s looking for something that has aeiou -- exactly like that. If you want something that ll look for either a, e, i, o, or u in each line, then
      Message 2 of 10 , Dec 2, 2004
      • 0 Attachment
        On Thu, 02 Dec 2004 23:53:52 -0000, Jeff Shu <santa98bn@...> wrote:
        >
        >
        > Hello everyone:
        >
        > Hello
        >
        > I am doing a exercise which requires me to find out all the words
        > that contains five vowels(a, e, I, o, u) from a text file.
        >
        > I wrote a the following code to test it.
        > While(<>)
        > {
        > print if /aeiou/
        > }
        >
        > but the result didn't show any words that contains five vowels. I
        > am
        > wondering what's wrong the my regular expression. Please give me
        > some
        > suggestions. Thanks!
        > Jeff
        > 12-02-04
        >

        Your code's looking for something that has "aeiou" -- exactly like that.

        If you want something that'll look for either a, e, i, o, or u in each
        line, then you need:
        while(<>)
        {
        print if /[aeiou]/
        }

        If you want something that'll look for all five vowels in one line,
        all i could think of is this:
        while(<>)
        {
        print if /a/ and /e/ and /i/ and /o/ and /u/
        }

        HTH

        --
        http://www.bloodpet.tk/
        If only one could get that wonderful feeling of accomplishment without
        having to accomplish anything.
      • Ron Goral
        ... Another method would be something like: for a single word - /a w?e w?i w?o w?u/; for a line of text with any chars in between - /a.?e.?i.?o.?u/ Peace - Ron
        Message 3 of 10 , Dec 2, 2004
        • 0 Attachment
          > -----Original Message-----
          > From: eman [mailto:bloodpet@...]
          > Sent: Thursday, December 02, 2004 6:34 PM
          > To: perl-beginner@yahoogroups.com; Shu, Jeff
          > Subject: Re: [PBML] Usage of regular expression?
          >
          >
          >
          > On Thu, 02 Dec 2004 23:53:52 -0000, Jeff Shu <santa98bn@...> wrote:
          > >
          > >
          > > Hello everyone:
          > >
          > > Hello
          > >
          > > I am doing a exercise which requires me to find out all the words
          > > that contains five vowels(a, e, I, o, u) from a text file.
          > >
          > > I wrote a the following code to test it.
          > > While(<>)
          > > {
          > > print if /aeiou/
          > > }
          > >
          > > but the result didn't show any words that contains five vowels. I
          > > am
          > > wondering what's wrong the my regular expression. Please give me
          > > some
          > > suggestions. Thanks!
          > > Jeff
          > > 12-02-04
          > >
          >
          > Your code's looking for something that has "aeiou" -- exactly like that.
          >
          > If you want something that'll look for either a, e, i, o, or u in each
          > line, then you need:
          > while(<>)
          > {
          > print if /[aeiou]/
          > }
          >
          > If you want something that'll look for all five vowels in one line,
          > all i could think of is this:
          > while(<>)
          > {
          > print if /a/ and /e/ and /i/ and /o/ and /u/
          > }
          >
          > HTH

          Another method would be something like:

          for a single word -
          /a\w?e\w?i\w?o\w?u/;

          for a line of text with any chars in between -

          /a.?e.?i.?o.?u/

          Peace -
          Ron
        • eman
          On Thu, 2 Dec 2004 18:55:25 -0600, Ron Goral ... But wouldn t those require that the vowels (a, e, i, o, u) are in that order? -- http://www.bloodpet.tk/ An
          Message 4 of 10 , Dec 2, 2004
          • 0 Attachment
            On Thu, 2 Dec 2004 18:55:25 -0600, Ron Goral
            <subscriptions@...> wrote:
            >
            >
            > Another method would be something like:
            >
            > for a single word -
            > /a\w?e\w?i\w?o\w?u/;
            >
            > for a line of text with any chars in between -
            >
            > /a.?e.?i.?o.?u/
            >
            > Peace -
            > Ron
            >
            >

            But wouldn't those require that the vowels (a, e, i, o, u) are in that order?

            --
            http://www.bloodpet.tk/
            An effective way to deal with predators is to taste terrible.
          • Ron Goral
            ... Ummmmmmmmmmmm Good thing that wasn t production code. (weak smile). Please disregard this ill-considered contribution.
            Message 5 of 10 , Dec 2, 2004
            • 0 Attachment
              > -----Original Message-----
              > From: eman [mailto:bloodpet@...]
              > Sent: Thursday, December 02, 2004 7:08 PM
              > To: perl-beginner@yahoogroups.com; Goral, Ron
              > Subject: Re: [PBML] Usage of regular expression?
              >
              >
              >
              > On Thu, 2 Dec 2004 18:55:25 -0600, Ron Goral
              > <subscriptions@...> wrote:
              > >
              > >
              > > Another method would be something like:
              > >
              > > for a single word -
              > > /a\w?e\w?i\w?o\w?u/;
              > >
              > > for a line of text with any chars in between -
              > >
              > > /a.?e.?i.?o.?u/
              > >
              > > Peace -
              > > Ron
              > >
              > >
              >
              > But wouldn't those require that the vowels (a, e, i, o, u) are in
              > that order?

              Ummmmmmmmmmmm

              Good thing that wasn't production code. (weak smile).

              Please disregard this ill-considered contribution.
            • Ron Goral
              ... K. This one I tried first. ;P # All the vowels, not in order my $text = qq[Every good boy deserves favor.]; # All except a, not in order my $other =
              Message 6 of 10 , Dec 2, 2004
              • 0 Attachment
                > -----Original Message-----
                > From: eman [mailto:bloodpet@...]
                > Sent: Thursday, December 02, 2004 7:08 PM
                > To: perl-beginner@yahoogroups.com; Goral, Ron
                > Subject: Re: [PBML] Usage of regular expression?
                >
                >
                >
                > On Thu, 2 Dec 2004 18:55:25 -0600, Ron Goral
                > <subscriptions@...> wrote:
                > >
                > >
                > > Another method would be something like:
                > >
                > > for a single word -
                > > /a\w?e\w?i\w?o\w?u/;
                > >
                > > for a line of text with any chars in between -
                > >
                > > /a.?e.?i.?o.?u/
                > >
                > > Peace -
                > > Ron
                > >
                > >
                >
                > But wouldn't those require that the vowels (a, e, i, o, u) are in
                > that order?

                K. This one I tried first. ;P


                # All the vowels, not in order
                my $text = qq[Every good boy deserves favor.];
                # All except a, not in order
                my $other = qq[bcdfeghijko if You think of it.];

                if ($text =~ /([AEIOUaeiou])/g){print qq[Got text - $1.] if $1;}
                if ($other =~ /[AEIOUaeiou]/g){print qq[Got other - $1.] if $1;}

                Output -

                Got text - E.
              • Charles K. Clarkson
                ... Why use the /g modifier in an if ? The first match is all we need for truth. Other matches don t matter. Also $1 will never be true if the test fails.
                Message 7 of 10 , Dec 2, 2004
                • 0 Attachment
                  Ron Goral <subscriptions@...> wrote:

                  : K. This one I tried first. ;P
                  :
                  :
                  : # All the vowels, not in order
                  : my $text = qq[Every good boy deserves favor.];
                  : # All except a, not in order
                  : my $other = qq[bcdfeghijko if You think of it.];
                  :
                  : if ($text =~ /([AEIOUaeiou])/g){print qq[Got text - $1.] if $1;}

                  Why use the /g modifier in an 'if'? The first match
                  is all we need for truth. Other matches don't matter. Also
                  $1 will never be true if the test fails.

                  print qq[Got text - $1.] if $text =~ /([AEIOUaeiou])/;


                  : if ($other =~ /[AEIOUaeiou]/g){print qq[Got other - $1.] if $1;}

                  There is no capture here. $1 will always be false.
                  Nothing will ever print.


                  HTH,

                  Charles K. Clarkson
                  --
                  Mobile Homes Specialist
                  254 968-8328
                • Ron Goral
                  ... Correct about the g modifier. But, this is not a complete program, just an illustration of how to capture the match. What to do when it matches or not is
                  Message 8 of 10 , Dec 2, 2004
                  • 0 Attachment
                    > -----Original Message-----
                    > From: Charles K. Clarkson [mailto:cclarkson@...]
                    > Sent: Thursday, December 02, 2004 7:51 PM
                    > To: perl-beginner@yahoogroups.com
                    > Subject: RE: [PBML] Usage of regular expression?
                    >
                    >
                    >
                    > Ron Goral <subscriptions@...> wrote:
                    >
                    > : K. This one I tried first. ;P
                    > :
                    > :
                    > : # All the vowels, not in order
                    > : my $text = qq[Every good boy deserves favor.];
                    > : # All except a, not in order
                    > : my $other = qq[bcdfeghijko if You think of it.];
                    > :
                    > : if ($text =~ /([AEIOUaeiou])/g){print qq[Got text - $1.] if $1;}
                    >
                    > Why use the /g modifier in an 'if'? The first match
                    > is all we need for truth. Other matches don't matter. Also
                    > $1 will never be true if the test fails.
                    >
                    > print qq[Got text - $1.] if $text =~ /([AEIOUaeiou])/;
                    >
                    >
                    > : if ($other =~ /[AEIOUaeiou]/g){print qq[Got other - $1.] if $1;}
                    >
                    > There is no capture here. $1 will always be false.
                    > Nothing will ever print.
                    >
                    >
                    > HTH,
                    >
                    > Charles K. Clarkson
                    > --
                    > Mobile Homes Specialist
                    > 254 968-8328

                    Correct about the g modifier. But, this is not a complete program, just an
                    illustration of how to capture the match. What to do when it matches or not
                    is up to the implementor.
                  • J.E. Cripps
                    ... all five? or words with any selection (aaiio, aeeou, eeeio)? ... Hint: /cat/ will match catalogue , concatenate wildcat ... this is IIRC perlrequick
                    Message 9 of 10 , Dec 2, 2004
                    • 0 Attachment
                      > I am doing a exercise which requires me to find out all the words
                      > that contains five vowels(a, e, I, o, u) from a text file.

                      all five? or words with any selection (aaiio, aeeou, eeeio)?


                      > I wrote a the following code to test it.
                      > While(<>)
                      > {
                      > print if /aeiou/
                      > }

                      Hint: /cat/ will match "catalogue", "concatenate"
                      "wildcat" ... this is IIRC perlrequick and perlretut.
                      (the perldocs for regular expressions)
                    • Mike Flannigan
                      ... I would do: while ( ) { next unless tr/a//; next unless tr/e//; next unless tr/i//; next unless tr/o//; next unless tr/u//; print; } Mike
                      Message 10 of 10 , Dec 3, 2004
                      • 0 Attachment
                        perl-beginner@yahoogroups.com wrote:

                        >
                        > Date: Thu, 02 Dec 2004 23:53:52 -0000
                        > From: "Jeff Shu" <santa98bn@...>
                        > Subject: Usage of regular expression?
                        >
                        > Hello everyone:
                        >
                        > Hello
                        >
                        > I am doing a exercise which requires me to find out all the words
                        > that contains five vowels(a, e, I, o, u) from a text file.
                        >
                        > I wrote a the following code to test it.
                        > While(<>)
                        > {
                        > print if /aeiou/
                        > }
                        >
                        > but the result didn't show any words that contains five vowels. I
                        > am
                        > wondering what's wrong the my regular expression. Please give me
                        > some
                        > suggestions. Thanks!
                        > Jeff
                        > 12-02-04

                        I would do:

                        while (<>) {
                        next unless tr/a//;
                        next unless tr/e//;
                        next unless tr/i//;
                        next unless tr/o//;
                        next unless tr/u//;
                        print;
                        }


                        Mike
                      Your message has been successfully submitted and would be delivered to recipients shortly.