Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

regular expressions

by asaf (Initiate)
on Mar 19, 2003 at 03:07 UTC ( #244221=perlquestion: print w/replies, xml ) Need Help??

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

I need to know how can i find all the words that end with OMA but not with STOROMA

Replies are listed 'Best First'.
Re: regular expressions
by Enlil (Parson) on Mar 19, 2003 at 04:23 UTC
    one way to do it is with a look behind assertion in a regular expression. (i.e. (?<!pattern) like the following:
    while ( <DATA> ) { print if ( m/(?<!STOR)OMA$/ ); } __DATA__ OMA COMA STORAMA HISTOROMA PUMA MELANOMA STOROMA

    where m/(?<!STOR)OMA$/ pretty much looks for a pattern exactly as you described.

    -enlil

Re: regular expressions
by dvergin (Monsignor) on Mar 19, 2003 at 06:08 UTC
    If you are just beginning to learn regexes, the negative look-behind examples given in the other posts here may feel a little challenging. Here's an approach that uses more basic regex functionality.
    #!/usr/bin/perl -w use strict; my @words = qw(COMA STORAMA HISTOROMA PUMA MELANOMA STOROMA); for my $str ( @words ) { if ($str =~ /OMA$/ and $str !~ /STOROMA$/ ) { print "$str\n"; } }

    ------------------------------------------------------------
    "Perl is a mess and that's good because the
    problem space is also a mess.
    " - Larry Wall

Re: regular expressions
by thelenm (Vicar) on Mar 19, 2003 at 04:17 UTC
    Use negative lookbehind in a regular expression, like this:
    /(?<!STOR)OMA\z/

    -- Mike

    --
    just,my${.02}

Re: regular expressions
by greenFox (Vicar) on Mar 19, 2003 at 04:10 UTC
    perlman:perlre could be a good place to start. The Monastery also has a good number of beginners tutorials.

    --
    Life is a tale told by an idiot -- full of sound and fury, signifying nothing. William Shakespeare, Macbeth

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://244221]
Approved by graff
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2023-01-30 04:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?