Socrates440 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to reverse engineer two scripts that my perl programming book gave me but I can't find explanations in the book (I am sure they are there, but I am not having any luck) for some of the special characters. The first script is:
while (<STDIN>) { if (/a/i && /e/i && /i/i && /o/i && /u/i) { print; } }
My question for the above script is what is the i outside of each of the // symbles. The second script is:
hile (<>) { print if (/^[^aeiou]*a[^eiou]*e[^aiou]*i[^aeou]*o[^aeiu]*u[^aeio]*$ ); }
For this script I have a few more questions. It is supposed to parse a statement in order to determine if it shows each of the vowels in order. Why couldn't one just do this /a*i*o*u*/. Also what does e^aiou actually look for? e with any vowel character after it? Last, what is the purpose of the $ sign. I found this code very confusing. Thanks for your help!

Replies are listed 'Best First'.
Re: Regular Expression
by toolic (Bishop) on Jun 21, 2012 at 02:20 UTC
Re: Regular Expression
by roboticus (Chancellor) on Jun 21, 2012 at 01:50 UTC

    Socrates440:

    Have you read perldoc perlre? (See what I did there? The Socratic method in action!)

    Seriously, though, after the close of a regular expression, you can give it flags for things like case (i)nsensitive matching, as described in the aforementioned document.

    As for the questions regarding the second statement, perlre answers those as well.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Regular Expression
by frozenwithjoy (Priest) on Jun 21, 2012 at 02:31 UTC
    Take a look at this handy little cheatsheet. When I was learning regex, I put something like this as my desktop for ultra quick reference. (DISCLAIMER: This regex cheatsheet is not perl-specific, but is still helpful for learning regexs in general. As always, be sure to test your patterns.)

      Thanks, this is useful. However, it should come with two caveats for the unwary:

      1. This cheat sheet is not Perl-specific. For example, there is no /U modifier in Perl for ‘ungreedy patterns.’
      2. See comment #40 by merlyn: “Your email regex is wrong” (referring to a sample pattern on the png version of the cheat sheet).

      Athanasius <°(((><contra mundum

        After reading the resources that you all posted, and re-reading my chapter, I have mangaed to narrow my question. Obviously, the way that I am reading this code is incorrect because I don't think that it should work but when I tested it I found that it did. Please tell me where my error in thinking is. It comes straight from my perl book. It is supposed to parse a string in order to determine if the string contains aeiou in order.
        #!/usr/bin/perl -w while (<>) { print if (/^[^aeiou]*a[^eiou]*e[^aiou]*i[^aeou]*o[^aeiu]*u[^aeio]*$/); }
        It is my understanding that the code should match if and only if there is a non vowel character at the beginning of the string, an a followed by any number of characters except for other vowels, an e followed by any number of characters except for any number of vowels etc... and finally a u followed by any number of characters that are not other vowels at the end of the string. My questions: -The ^ anchor before the first set of brackets seems to me to say that the string must begin with a non vowel character in order to match. I tested this theory and as long as the vowels are in order the string can begin with a non vowel character. How does that work? -Why does the if statement not require brackets? -Why is the $ anchor necessary at the end? Wouldn't the regular expression do the same thing if it were left off? Thanks!!!