![]() |
Regular Expressions
Code:
#!/usr/bin/perl |
you don't need that much regular expression. just split on space
Code:
@s = split /\s+/ , $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:
Color coded (not shown: the anchors at the beginning and the end; whitespaces shown as '·'): Code:
^(\w+).*\s(\w+)$ |
try:
Code:
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 ---------- Quote:
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 |
Quote:
(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:
$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
|
| All times are GMT -4. The time now is 04:07 AM. |
Powered by: vBulletin,
Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2013. All Rights Reserved.
Forum Operations by The UNIX and Linux Forums