more regex woes...
Hi all, I am brand new to perl, and programming at that. I'm learning alot from Robert's Perl Tutorial and I was hoping someone culd possibly clear up the following from the tutorial: $_='My email address is <[EMAIL PROTECTED]>.'; print "Match 1 worked :$1:" if /(<*)/i; $_='.'; print "Match 2 worked :$1:" if /(<*)/i; $_='My email address is <[EMAIL PROTECTED]>.'; print "Match 3 worked :$1:" if /(<*>)/i; Match 1 is true. It doesn't return anything, but it is true because there are 0 < at the very start of the string. Match 2 works. After the 0 < at the start of the string, there is 1 < so the regex can match that too. Match 3 works. After the failing on the first < , it jumps to the second. After that, there are plenty more to match right up until the required ending. I've been doing fine so far but I just can't seem to completly grasp this part. thanx, joe -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: more regex woes...
Looks to me like you understand it better than I did. I don't understand your question. /\/\ark -Original Message- From: Joe Sprankle [mailto:[EMAIL PROTECTED]] Sent: Wednesday, July 31, 2002 10:44 AM To: [EMAIL PROTECTED] Subject: more regex woes... Hi all, I am brand new to perl, and programming at that. I'm learning alot from Robert's Perl Tutorial and I was hoping someone culd possibly clear up the following from the tutorial: $_='My email address is <[EMAIL PROTECTED]>.'; print "Match 1 worked :$1:" if /(<*)/i; $_='.'; print "Match 2 worked :$1:" if /(<*)/i; $_='My email address is <[EMAIL PROTECTED]<<<<>.'; print "Match 3 worked :$1:" if /(<*>)/i; Match 1 is true. It doesn't return anything, but it is true because there are 0 < at the very start of the string. Match 2 works. After the 0 < at the start of the string, there is 1 < so the regex can match that too. Match 3 works. After the failing on the first < , it jumps to the second. After that, there are plenty more to match right up until the required ending. I've been doing fine so far but I just can't seem to completly grasp this part. thanx, joe -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: more regex woes...
> -Original Message- > From: Joe Sprankle [mailto:[EMAIL PROTECTED]] > Sent: Wednesday, July 31, 2002 1:44 PM > To: [EMAIL PROTECTED] > Subject: more regex woes... > > > Hi all, > I am brand new to perl, and programming at that. I'm learning > alot from > Robert's Perl Tutorial and I was hoping someone culd possibly > clear up the > following from the tutorial: What is "Robert's Perl Tutorial"? > > $_='My email address is <[EMAIL PROTECTED]>.'; > print "Match 1 worked :$1:" if /(<*)/i; > > $_='.'; > print "Match 2 worked :$1:" if /(<*)/i; > > $_='My email address is <[EMAIL PROTECTED]<<<<>.'; > print "Match 3 worked :$1:" if /(<*>)/i; > > Match 1 is true. It doesn't return anything, but it is true > because there are 0 < at the very > start of the string. > Match 2 works. After the 0 < at the start of the string, > there is 1 < so the > regex can match that too. > Match 3 works. After the failing on the first < , it jumps to > the second. > After that, there are plenty more to match right up until the required > ending. > I've been doing fine so far but I just can't seem to > completly grasp this > part. What don't you understand? Why they capture what they do? They all capture the leftmost, longest substring matching the pattern. An regex like /<*/ will *always* match at the beginning of the string, since '*' means "zero or more of the preceding item". So this regex will only capture <'s if they appear at the beginning of the string. Perhaps that's what "Robert" is trying to illustrate. The regex /<*>/ won't necessarily match at the beginning, because the zero or more <'s must be followed by one > char. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: more regex woes...
Joe Sprankle wrote: > > Hi all, Hello, > I am brand new to perl, and programming at that. I'm learning alot from > Robert's Perl Tutorial and I was hoping someone culd possibly clear up the > following from the tutorial: > > $_='My email address is <[EMAIL PROTECTED]>.'; > print "Match 1 worked :$1:" if /(<*)/i; This regular expression matches zero or more consecutive '<' characters case-insensitively and stores the match in the $1 variable. Since the '<' character doesn't have an upper or lower case version the /i option is not doing anything useful. Since zero '<' characters will always match, the regular expression is always true and the statement will always print. > $_='.'; > print "Match 2 worked :$1:" if /(<*)/i; Same as above. > $_='My email address is <[EMAIL PROTECTED]>.'; > print "Match 3 worked :$1:" if /(<*>)/i; This regular expression matches zero or more consecutive '<' characters followed by one '>' character case-insensitively and stores the match in the $1 variable. Since neither the '<' or '>' character have an upper or lower case version the /i option is not doing anything useful. Since '>' must match for the regular expression to be true and the '*' is greedy this will match the longest string of '<' followed by a '>' at the end of your string. John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: more regex woes...
>What is "Robert's Perl Tutorial"? A good perl tutorial that I heard of on this list after lurking for awhile. I believe the link is http://www.sthomas.net/roberts-perl-tutorial.htm >What don't you understand? Why they capture what they do? >They all capture the leftmost, longest substring matching the >pattern. >An regex like /<*/ will *always* match at the beginning of the >string, since '*' means "zero or more of the preceding item". >So this regex will only capture <'s if they appear at the beginning >of the string. Perhaps that's what "Robert" is trying to illustrate. >The regex /<*>/ won't necessarily match at the beginning, because >the zero or more <'s must be followed by one > char. I got it now, thanks for the reply. joe -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]