Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

First word

by Anonymous Monk
on Apr 24, 2008 at 08:50 UTC ( #682581=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys, I have a string $x in a scalar, and I need to copy the first 'word' of string to $y and I can't seem to find anywhere how to do it. I know that \w would be the regex for a 'word' and know how to match it, but how do I capture it in another variable without having to split the whole string then just copy $x[0]. Many thanks, Bill

Comment on First word
Select or Download Code
Re: First word
by FunkyMonk (Canon) on Apr 24, 2008 at 08:56 UTC
    Step-by-step:

    match a single word character/\w/
    match a word /\w+/
    match a word at the start /^\w+/
    capture it /^(\w+)/
    bind the pattern to a string $x =~ /^(\w+)/
    assign captured word my ( $word ) = $x =~ /^(\w+)/

    The parentheses around $word are important.

    Update: Reformatted


    Unless I state otherwise, my code all runs with strict and warnings
Re: First word
by andreas1234567 (Vicar) on Apr 24, 2008 at 08:56 UTC
    Like this?
    $ perl -l use warnings; use strict; my $x = "hey dude what's up"; (my $y = $x) =~ s/(\w+).*/$1/; print $y; __END__ hey
    --
    No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]
      Winner winner, chicken dinner! (saw the movie 21 last night, it comes highly recommended). Yes thats perfect! Just so I can make sense of it and use it in future, any chance you could explain s/(\w+).*/$1/ Thanks again, it's perfect.
        You can read all about the Substitution Operator in the free online documentation at perlretut. Search for "Search and replace". There are dazzling multi-color code examples complete with hyperlinks!

        Or, if you prefer reading from a book, Learning Perl is a good place to start.

Re: First word
by haoess (Curate) on Apr 24, 2008 at 08:56 UTC
    $ perl -wle 'print shift =~ /^(\w+)\b/' "That's my sentence." That

    As you can see, \w matches a word character, and \b marks the end of the first word (since we're matching the beginning aka ^ of the string).

    More information at perlre.

    -- Frank

      You don't need \b because \w+ is greedy.
Re: First word
by cdarke (Prior) on Apr 24, 2008 at 12:26 UTC
    I know that \w would be the regex for a 'word'

    I suppose that depends on what you mean by a 'word'. \w is [A-Za-z0-9_], it includes numerics and the underscore. As noted, \b is a word boundary, although sometimes matches in unexpected places like apostrophies - "Clive's house" will appear to be 3 words.
Re: First word
by jeepj (Scribe) on Apr 24, 2008 at 12:50 UTC
    you could also use split (There's more than one way to do it)
    $y=split(/\s/,$x)[0];
    Update: at first, it sounds a good solution, but I'm not sure after all...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2015-07-06 02:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (69 votes), past polls