Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

pick a word contaning reg-ex

by keesturam (Initiate)
on Dec 04, 2012 at 13:22 UTC ( [id://1007086]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks, I have started learning perl and was trying to do some exercise in regular expressions. One of the exercise was this.. "Construct regular expressions which can pick out words from a file with the Words containing an exclamation mark" I can write a pgm to find if there is an exclamation in a line/file. But how do I extract the word containing a reg ex (exclamation as in this case)? I tried googling, but couldn't find a suitable answer. --Karthick

Replies are listed 'Best First'.
Re: pick a word contaning reg-ex
by 2teez (Vicar) on Dec 04, 2012 at 13:37 UTC

    You mean something like this:

    use warnings; use strict; while(<DATA>){ chomp; print grep{/.+?\!/} split } __DATA__ Mary had a little lamb! , whose fleece! was white! as snow. And everywhere! that Mary went, the lamb was sure! to go.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: pick a word contaning reg-ex
by Kenosis (Priest) on Dec 04, 2012 at 16:19 UTC

    And another option:

    use Modern::Perl; while (<DATA>) { say $1 while /(\S+!)/g; } __DATA__ Mary had a little lamb! , whose fleece! was white! as snow. And everywhere! that Mary went, the lamb was sure! to go.

    Output:

    lamb! fleece! white! everywhere! sure!
Re: pick a word contaning reg-ex
by Wiggins (Hermit) on Dec 04, 2012 at 17:16 UTC
    The problem says "Words containing an exclamation mark". This is not the same as "ending with".
    /(\w*!\w*)/
    This is flawed because it matches "!".
    I don't know how to specify at least 1 sub-part must have a length > 0.

    It is always better to have seen your target for yourself, rather than depend upon someone else's description.

      #!/perl use warnings; use strict; while(<DATA>){ print map {"$_\n"} grep{/!/ and /[^!]/} split; } __DATA__ Mary had a little lamb! , whose fleece! was white! as snow. And everywhere! that Mary went, the lamb was sure! to go. oops!eedaisy ! ! !! HI!!IH
Re: pick a word contaning reg-ex
by keesturam (Initiate) on Dec 04, 2012 at 13:31 UTC

    Oh.! Reply from previous post answers my question as well!!

    perl -ne '/(\w*!\w*)/g && print "$1\n";' code_file(s)

    Lesson learnt. search for existing threads before posting questions :-)

        I just tried the code above and it only prints the first word (update: on each line) that contains an exclamation point.

        #!/perl use warnings; use strict; while(<DATA>){ #print map {"$_\n"} grep{/!/} split; /(\w*!\w*)/g && print "$1\n"; } __DATA__ Mary had a little lamb! , whose fleece! was white! as snow. And everywhere! that Mary went, the lamb was sure! to go. oops!eedaisy

        Produces this:

        lamb! fleece! everywhere! sure! oops!eedaisy
Re: pick a word contaning reg-ex
by sundialsvc4 (Abbot) on Dec 04, 2012 at 16:57 UTC

    There are several things that you should, yourself, search for answers to.   First, how to write a regular expression that searches for “one-or-more Word characters followed by an exclamation mark.”   Then, the meaning of the idea “greedy,” and how to specify it.   Finally, how to perform more than one regular-expression match (in a loop ...) within a single string.

    The information is there e.g. in perldoc perlre.   If patiently read from stem to stern.

      Thanks..!! This was really helpful.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-24 04:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found