ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Hi all,
Trying to get this prog to match exactly 3 vowels in a string. Here is my code but it doesn't wrok and should! Read 'perldoc perlre' and it says to use what I ahve used for matching 'at least n times but no more than m times, where n and m are like so : {n, m}.
heres the code:
Code:
#!/usr/bin/perl -w
use strict;
# a program to test for a sequence of at least 3 vowels
# declare variables
my $string;
print("\n\nThis program will check to see if your word contains\n");
print("a sequence of exactly 3 vowels.\n");
print("The program will tell you if there is a match or not!\n");
print("You may continue entering single words to test until CTRL-D.\n\n");
while ($string = <STDIN> ){
chomp ($string);
if ($string =~ m/[aeiou]{3,3}?/) {
print ("We have a match\n\n");
} else {
print ("Nope!\n\n");
}
}
the regular expression allows for any number of characters to come before the 3 vowel sequence ('.*') and then allows for three vowels upper or lower case and then any number of trailing characters ('.*').
Perhaps you could try something like the following? (NOTE: Untested)
Code:
#!/usr/bin/perl -w
use strict;
# a program to test for a sequence of at least 3 vowels
# declare variables
my $string;
print("\n\nThis program will check to see if your word contains\n");
print("a sequence of exactly 3 vowels.\n");
print("The program will tell you if there is a match or not!\n");
print("You may continue entering single words to test until CTRL-D.\n\n");
while (<STDIN> ){
$string = chomp;
if ($string =~ /[aeiou]{3}/) {
print ("We have a match\n\n");
} else {
print ("Nope!\n\n");
}
}
You can always do {3} instead of {3,3} -- it tells the parser to find *exactly* three.
I'm not sure if this will work or not, however, as I have not tested it.
#!/usr/bin/env python
import re
import sys
line = sys.stdin.readline()
while line:
if re.search('[aeiou]{3}', line, re.IGNORECASE):
print "Match!"
else:
print "No match!"
line = sys.stdin.readline()
it will not match on 2 vowels, it WILL match on 3 vowels, but it wil lalso match on 4 vowels!!!
I should have seen that coming A sequence of 4 vowels does contain a sequence of 3, so it matches on the first 3. (A clearer definition of "does not work" might have helped someone answer sooner)
You can either do something like /(^|[^aeiou])[aeiou]{3}([^aeiou]|$)/, recognizing that the two adjacent characters may be included in $&, or you can use negative lookahead and lookbehind assertions. I don't remember the syntax for those so look them up This second solution will be cleaner.
I should mention that (?: ...) does the same as ( ...) as long as you aren't using the $1, $2 etc variables afterwards, and is faster. It's also less clear :/
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.