The UNIX and Linux Forums

The UNIX and Linux Forums (http://www.unix.com/index.php)
-   Shell Programming and Scripting (http://www.unix.com/shell-programming-and-scripting/)
-   -   Regular Expressions (http://www.unix.com/shell-programming-and-scripting/122679-regular-expressions.html)

DavidHe 11-02-2009 09:09 AM

Regular Expressions
 
Code:

#!/usr/bin/perl


$word = "one last challenge";


if ( $word  =~ /^(\w+).*\s(\w+)$/ )
{
        print "$1";
        print "\n";
        print "$2";

}

The output shows that "$1" is with result one and "$2" is with result challenge. I am confused about how this pattern match expression works step by step. I request u guys` explanation. Thank u very much.

ghostdog74 11-02-2009 09:41 AM

you don't need that much regular expression. just split on space
Code:

@s = split /\s+/ , $word;
then go through each element of @s to get your word.

pludi 11-02-2009 10:14 AM

I guess the OPs question isn't about splitting a string, but rather about how regular expressions work (in this case in Perl).

Let's take a look at the regex you have:
  • ^
    Start (Anchor) at the beginning of the string
  • (\w+)
    Match one or more word character greedily, and save the match in a variable for further use.
  • .*
    Match 0 or more of any character greedily, and discard the result.
  • \s
    Match exactly 1 whitespace character
  • (\w+)
    Again, match one or more word character and save the result.
  • $
    Anchor the regex at the end of the string.

Color coded (not shown: the anchors at the beginning and the end; whitespaces shown as '·'):
Code:

^(\w+).*\s(\w+)$
one·last·challenge


sweetblood 11-02-2009 10:23 AM

try:
Code:

printf %s, (split /\s/, $word)[0]

DavidHe 11-02-2009 06:18 PM

Hi thank u for your reply. I juz get confused that why ".*\s" does not match "last " Thank you
note that there is a whitespace after double-quoted last

---------- Post updated at 09:18 AM ---------- Previous update was at 09:17 AM ----------

Quote:

Originally Posted by pludi (Post 302367330)
I guess the OPs question isn't about splitting a string, but rather about how regular expressions work (in this case in Perl).

Let's take a look at the regex you have:
  • ^
    Start (Anchor) at the beginning of the string
  • (\w+)
    Match one or more word character greedily, and save the match in a variable for further use.
  • .*
    Match 0 or more of any character greedily, and discard the result.
  • \s
    Match exactly 1 whitespace character
  • (\w+)
    Again, match one or more word character and save the result.
  • $
    Anchor the regex at the end of the string.

Color coded (not shown: the anchors at the beginning and the end; whitespaces shown as '·'):
Code:

^(\w+).*\s(\w+)$
one·last·challenge




Hi thank u for your reply. I juz get confused that why ".*\s" does not match "last " Thank you
note that there is a whitespace after double-quoted last

durden_tyler 11-02-2009 08:39 PM

Quote:

Originally Posted by DavidHe (Post 302367459)
...
Hi thank u for your reply. I juz get confused that why ".*\s" does not match "last " Thank you
note that there is a whitespace after double-quoted last

It's pretty simple.

(A) Yes, ".*\s" does match " last ". (i.e. "last" with a space at both ends).
(B) It matched, but was not stored in a variable.

Why not ? Because it was not enclosed within brackets.

If you enclose it within brackets, then $2 will be assigned the value " last ". And, of course, "challenge" will go to $3.

The test is shown below:

Code:

$
$ cat -n tst.pl
    1  #!/usr/bin/perl
    2  $word = "one last challenge";
    3  if ( $word  =~ /^(\w+)(.*\s)(\w+)$/ )
    4  {
    5    print "==>|$1|<==";    # ==>|one|<==
    6    print "\n";
    7    print "==>|$2|<==";    # ==>| last |<==
    8    print "\n";
    9    print "==>|$3|<==";    # ==>|challenge|<==
    10  }
    11
$
$ perl tst.pl
==>|one|<==
==>| last |<==
==>|challenge|<==$
$
$

Note that the value of $2 is "last" with a single space at both ends.
Also note that $3 equals "challenge" without a newline at the end, due to which the $ prompt of the shell shows up right after those "==" characters.

HTH,
tyler_durden

DavidHe 11-03-2009 05:13 AM

Thank u for your reply.... But doesn`t anything match the pattern should be stored in $1,$2...$n???? U said it is matched but not stored in the $2 because it is not enclsoed within bracket....So what do u by that? I am new to Perl thank u ....for ur reply


All times are GMT -4. The time now is 08:13 AM.

Linux and Unix Supported by: vBulletin
Search Engine Optimisation provided by DragonByte SEO v1.1.4 (Pro) - vBulletin Mods & Addons Copyright © 2014 DragonByte Technologies Ltd.
The UNIX and Linux Forums Content Copyright ©1993-2013. All Rights Reserved.
Forum Operations by The UNIX and Linux Forums