On 4/25/06, Monomachus <[EMAIL PROTECTED]> wrote:
> I need a RegEx (!!!with modify(m//)) what matches the words what ends with 
> 'a'.
> =============================
> I have a test_file (for ex.:)
> =============================
> beforematchafter
> wilma and nana
> Mrs. Wilma Flinstone hommie
> barney
> wilma&fred
> wilmafred
> flinstone
 > mama&fred
> ==============================
> my_program.pl
> ==============================
> #!/usr/bin/perl -w
> use strict;
> my $i=1;
> while (<>) {            # take one input line at time
>         chomp;
>         if (/(\b\w*a\b)/) {
>                 print "$1 found in line $i\n";
>                 $i++;
>         } else {
>                 $i++;
>         }
> }
> ==================================
> This program does not match 'nana' line 2 (only wilma) and it DOES atch 
> 'wilma&fred' line 5 and line 8 (I think it shouldn't)...
> And is the sign '&' in \w ?? I thought that \w == [a-zA-Z0-9_] or it isn't???
> ==================================

It doesn't match 'nana' because you aren't using the 'g' modifier, so
it exits after the first match and moves onto the next line. And
you're only printing $1 in any case, so even it it matched more, you
wouldn't know:

    if ( my @matches = /(\b\w*a\b)/g ) {
        print "@matches found in line $.\n";
    }

Also note the use of '$.'  You don't need to keep track of line
numbers. Perl does it for you.

As for wilma&fred, no, '&' isn't in \w. the regex only captures
'wilma' in 'wilma&fred'. Because '&' isn't a \w character, there's a
\b between the 'a' in wilma and the '&'. if you want to make sure your
\b is at a space or the end of the string, make sure you anchor it:

    #!/usr/bin/perl
    use strict;
    use warnings;

    while (<DATA>) {            # take one input line at time
        chomp;
        if (my @matches = /\b(\w*a)\b(?:\s|\z)/g ) {
            print join(', ', @matches), " found in line $.\n";
       }
    }

    __END__
    beforematchafter
    wilma and nana
    Mrs. Wilma Flinstone hommie
    barney
    wilma&fred
    wilmafred
    flinstone
    mama&fred

HTH,

-- jay
--------------------------------------------------
This email and attachment(s): [  ] blogable; [ x ] ask first; [  ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com  http://www.dpguru.com  http://www.engatiki.org

values of β will give rise to dom!

Reply via email to