http://www.perlmonks.org?node_id=682581

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

Replies are listed 'Best First'.
Re: First word
by FunkyMonk (Chancellor) 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 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 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 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...