Regular Expressions

#!/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.

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

@s = split /\s+/ , $word;

then go through each element of @s to get your word.

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 '�'):

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

try:

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

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 ----------

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:

$
$ 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

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

First, please use proper English, not leetspeak or chat style abbreviations.

Second, it's all there. Regex, by default, match, but do not save what is matched. If you want to save the matches (extract them), you'll have to explicitly tell the state machine to do that, by enclosing the term to be saved in braces.

Also, Perl has a pretty good tutorial on regular expression, available via

perldoc perlretut

and online here

Thank you very much.
Second, it's all there. Regex, by default, match, but do not save what is matched. If you want to save the matches (extract them), you'll have to explicitly tell the state machine to do that, by enclosing the term to be saved in brace
This is really what I need. This is the point. Thank you so much!!!!!!!!!!!!!!!!!!!!!!!!!!!!!