Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
04-01-2004, 08:11 AM
|
#1
|
|
Member
Registered: Jul 2003
Location: NSW. Australia
Distribution: Ubuntu, Fedora, Slackware
Posts: 181
Rep:
|
pattern matching in perl
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");
}
}
it should be matching. hmm. any ideas???
|
|
|
|
04-01-2004, 09:02 AM
|
#2
|
|
Senior Member
Registered: Mar 2003
Location: Pittsburgh, PA
Distribution: Gentoo / NetBSD
Posts: 1,251
Rep:
|
try:
if ( $string=~m/.*[aeiouAEIOU]{3}.*/ )
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 ('.*').
Hope that helps,
jpbarto
|
|
|
|
04-01-2004, 12:15 PM
|
#3
|
|
Member
Registered: Mar 2004
Distribution: Slackware 9.1
Posts: 44
Rep:
|
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.
|
|
|
|
04-01-2004, 04:07 PM
|
#4
|
|
Member
Registered: Mar 2004
Location: Ann Arbor, MI
Distribution: Debian, SuSE
Posts: 41
Rep:
|
$string =~ m/(^|[^AEIOUaeiou])([AEIOUaeiou]{3})($|[^AEIOUaeiou])/
|
|
|
|
04-01-2004, 06:38 PM
|
#5
|
|
Member
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557
Rep:
|
Code:
echo -e 'foo\nbar\nbaaaz'|perl -ne 'print if /[aeiou]{3}/'
this prints just "baaaz". This is exactly what ssba has. It won't print "babaaz", so if you want that you need something different.
You don't need .* because perl accepts a match anywhere in the string: the match is not anchored to the ends unless you do so manually with ^ and $
What bothers me here is that the original poster's pattern seems to work fine: I don't understand what the '?' is doing.
|
|
|
|
04-01-2004, 10:43 PM
|
#6
|
|
Member
Registered: Jul 2003
Location: NSW. Australia
Distribution: Ubuntu, Fedora, Slackware
Posts: 181
Original Poster
Rep:
|
The ? is to make it a minimal match. For some strange reason, the original code DOES NOT WORK.
ie it will not match on 2 vowels, it WILL match on 3 vowels, but it wil lalso match on 4 vowels!!!
ie:
Code:
ae
Nope!
aei
We have a match
aeio
We have a match
aeiou
We have a match
aaa
We have a match
aa
Nope!
AAAAARRRRGGHHH!
and that is with {3}?
also the same for just {3}
and for {3,3}.
|
|
|
|
04-02-2004, 12:57 AM
|
#7
|
|
Member
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569
Rep:
|
Just for fun, here's a working python version:
Code:
#!/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()
|
|
|
|
04-02-2004, 01:04 AM
|
#8
|
|
Member
Registered: Jul 2003
Location: NSW. Australia
Distribution: Ubuntu, Fedora, Slackware
Posts: 181
Original Poster
Rep:
|
Dammit, why does your work with the {3} but mine doesn't????
|
|
|
|
04-02-2004, 09:51 AM
|
#9
|
|
Member
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557
Rep:
|
Quote:
|
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.
|
|
|
|
04-02-2004, 09:53 AM
|
#10
|
|
Member
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557
Rep:
|
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 :/
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:32 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|