LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 04-01-2004, 08:11 AM   #1
ludeKing
Member
 
Registered: Jul 2003
Location: NSW. Australia
Distribution: Ubuntu, Fedora, Slackware
Posts: 181

Rep: Reputation: 30
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???
 
Old 04-01-2004, 09:02 AM   #2
jpbarto
Senior Member
 
Registered: Mar 2003
Location: Pittsburgh, PA
Distribution: Gentoo / NetBSD
Posts: 1,251

Rep: Reputation: 45
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
 
Old 04-01-2004, 12:15 PM   #3
ssba
Member
 
Registered: Mar 2004
Distribution: Slackware 9.1
Posts: 44

Rep: Reputation: 15
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.
 
Old 04-01-2004, 04:07 PM   #4
mgatny
Member
 
Registered: Mar 2004
Location: Ann Arbor, MI
Distribution: Debian, SuSE
Posts: 41

Rep: Reputation: 15
$string =~ m/(^|[^AEIOUaeiou])([AEIOUaeiou]{3})($|[^AEIOUaeiou])/
 
Old 04-01-2004, 06:38 PM   #5
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
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.
 
Old 04-01-2004, 10:43 PM   #6
ludeKing
Member
 
Registered: Jul 2003
Location: NSW. Australia
Distribution: Ubuntu, Fedora, Slackware
Posts: 181

Original Poster
Rep: Reputation: 30
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}.
 
Old 04-02-2004, 12:57 AM   #7
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
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()
 
Old 04-02-2004, 01:04 AM   #8
ludeKing
Member
 
Registered: Jul 2003
Location: NSW. Australia
Distribution: Ubuntu, Fedora, Slackware
Posts: 181

Original Poster
Rep: Reputation: 30
Dammit, why does your work with the {3} but mine doesn't????
 
Old 04-02-2004, 09:51 AM   #9
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
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.
 
Old 04-02-2004, 09:53 AM   #10
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
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 :/
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl Pattern Matching Question pete1234 Programming 2 08-27-2005 10:26 AM
list files NOT matching a pattern smart_sagittari Linux - Newbie 9 05-20-2005 05:32 AM
perl regex matching exodist Programming 2 11-15-2004 10:50 PM
Pattern Matching Help in Bash script cmfarley19 Programming 1 04-07-2004 09:22 AM
Doubt in Pattern matching and vi editor.. suriyamohan Linux - General 2 08-19-2003 05:03 AM


All times are GMT -5. The time now is 12:31 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration