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

words wich ends with "f"

by Anonymous Monk
on Jun 19, 2008 at 15:12 UTC ( [id://692941]=perlquestion: print w/replies, xml ) Need Help??

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

hi
if i want to know the words wich begins with "f" this is my code:
#!/usr/bin/perl use strict; use warnings; $_="four fifth two frog left leaf fred wolf foul"; my @F=m/\bf.*?\b/g; print "@F";
the result is "four fifth frog fred" this is correct.
but if i want to know the words which ends with "f": using the code:
$_="four fifth two frog left leaf fred wolf"; my @F=m/\b.*?f\b/g; print "@F";
i get all the string "four fifth two frog left leaf fred wolf" and it must be "leaf wolf".
what is the right regex?
also i want to know the words wich does not begin with "f" but have at least one "f" in the remainder of the word:
$_="four fifth two frog left leaf fred wolf"; my @F=m/\b[^f].*?f.*?\b/g; print "@F";
the result "fifth two frog left leaf fred wolf"
and it must be "left leaf wolf"
can someone please give me a hint on the right regex here
thanks

Replies are listed 'Best First'.
Re: words wich ends with "f"
by moritz (Cardinal) on Jun 19, 2008 at 15:18 UTC
    but if i want to know the words which ends with "f"
    Mach exactly what you want, not more. A . matches non-word characters, which is bad in your case. Try \b\w*f\b instead.
    also i want to know the words wich does not begin with "f" but have at least one "f" in the remainder of the word:
    \b[^f]\w*f\w*\b might be worth a try, but I'm not convinced it's strict enough. Maybe \b(?=\w)[^f]\w*f\w*\b is right for you

    (All examples here untested).

    Update: ysth++ pointed out that (?=\w)[^f] is better written as [^f\W] - and indeed it is, if you consider only ASCII semantics. When you use Unicode, \w is not the same as [^\W].

      When you use Unicode, \w is not the same as [^\W].

      Really? Can you give an example?

        Really? Can you give an example?

        No, I tried to find one, and failed.

        I was referring to this section of perlunicode.

        (However, and as a limitation of the current implementation, using "\w" or "\W" inside a "[...]" character class will still match with byte semantics.)

        (taken from the perl 5.8.8 perlunicode man page). I tried to find an example for that with this script:

        use strict; for (1 .. 1e8){ eval { if ((chr($_) =~ m/\W/) xor (chr($_) =~ m/[^\w]/)){ print "Counter-example with chr($_)\n"; } } }

        But it didn't find anything.

        Did I misunderstood the docs? Or are the docs wrong/out of date?

Re: words wich ends with "f"
by psini (Deacon) on Jun 19, 2008 at 15:23 UTC

    You can try this:

    #!/usr/bin/perl use strict; use warnings; $_="four fifth two frog left leaf fred wolf foul"; my @F=m/\bf\w*/g; print "@F\n"; @F=m/\w*f\b/g; print "@F\n"; @F=m/\w+f\w+/g; print "@F\n";

    Results in:

    four fifth frog fred foul leaf wolf fifth left

    Update: the third case gives you all the words containing an "f" in the middle. To have all words containing but not beginning with an "f" use /[^f]\w*f\w*/g

    Update #2: this is better /\b[^f\W]\w*f\w*/g

    Careful with that hash Eugene.

Re: words wich ends with "f"
by b10m (Vicar) on Jun 19, 2008 at 15:23 UTC

    I'd go with something like this (yeah, TIMTOWTDI):

    $s = "four fifth two frog left leaf fred wolf"; @f = grep (/^[^f]\w*f\w*$/, split(' ', $s)); print "@f";
    --
    b10m

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-03-28 11:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found