[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re(3): [MacPerl] Regular Expression Definition



Michael,

I have been reading through the material on regular expressions in the
"Programming Perl" book (struggling is more like on the first read), and
need to read again, I did not understand the use of the $1, $2..
variables when they were first mentioned on pg. 64 (it seemed to come out
of nowhere, unless I missed their introduction earlier in the book). 
Your expanded example will be of great help in this regard...thanks very
much for your time and effort, the same goes for everyone else who has
responded to my inquiry.  This is my fist experience with a mailing list
and you all have made very valuable and pleasant.

Regards, Mark

--
Mark Yannuzzi
myannuzzi@aya.yale.edu

On Sat, Nov 7, 1998, Michael.Leahy@vt.edu <Michael.Leahy@vt.edu> wrote:

>Mark,
>
>At 11:46 AM -0800 11/7/98, Mark Yannuzzi mentioned:
>>>>$_="\012 YY,XX   BIN #  1-IDSXP   #  2-VGS1P   #  8-IGSO    # 10-VGF
>>>>\n";
>>>>
>>>># Preserve the #s and spaces
>>>>@numaray =
>>>>/\s*(\S+)\s+(\S+)\s+(#\s+\S+)\s+(#\s+\S+)\s+(#\s+\S+)\s+(#\s+\S+)/;
>>>># Ignore the #s and spaces
>>>>@justnums =
>>>>/\s*(\S+)\s+(\S+)\s+#\s+(\S+)\s+#\s+(\S+)\s+#\s+(\S+)\s+#\s+(\S+)/;
>>>
>>>This is a new construct to me, i.e., I have not seen what appears to be
>>>the match construct, used outside of $variable =~ m/<...>/.  It appears
>>>that there are multiple match criteria, and upon finding each in the
>>>line, places it in an element of the array, discarding anything that does
>>>not match (like the garbage character at the beginning of the line, and
>>>the ending newline).  I need to stare at it a little mre, but the single
>>>line solution is appealing.
>
>This is the same match command that you're used to, just using a few
>shortcuts.
>
>In the first line, I assigned the input string to $_.  The match command
>uses $_ as it's default.  In addition, since matches are so common, you
>don't even have to use the 'm' command at the beginning.
>
>One assumption is correct... each submatch within parentheses will be
>remembered and assigned to a variable (in this case, sequential elements of
>an array).
>
>Your statement about multiple match criteria, though, is not strictly
>correct.  The entire string is one match, but you want to remember some
>parts of the matched string and other parts you want to throw away.  All
>parts have to match or the entire string will be thrown away and nothing
>will match.
>
>
>Here's the same line rewritten with explicit variables and no shortcuts
>(putting the caret at the beginning speeds things up since the parser knows
>the string will only start at the beginning of a line):
>
>($xy, $bin, $col1, $col2, $col3, $col4) =
>  $_ =~
>m/^\s*(\S+)\s+(\S+)\s+(#\s+\S+)\s+(#\s+\S+)\s+(#\s+\S+)\s+(#\s+\S+)/;
>
>Or, you could reference the remembered submatches later:
>
>if (/^\s* (\S+)\s+(\S+)\s+(#\s+\S+)\s+(#\s+\S+)\s+(#\s+\S+)\s+(#\s+\S+)/) {
>	$xy = $1;
>	$bin = $2;
>	$col1 = $3;
>	$col2 = $4;
>	$col3 = $5;
>	$col4 = $6;
>}
>
># Remember just the #s and not the spaces between # and the string
>$count = /^\s*
>(\S+)\s+(\S+)\s+#\s+(\S+)\s+#\s+(\S+)\s+#\s+(\S+)\s+#\s+(\S+)/;
>if ($count > 0) {
>	$xy = $1;
>	$bin = $2;
>	$col1 = "# $3";
>	$col2 = "# $4";
>	$col3 = "# $5";
>	$col4 = "# $6";
>}
>
>--Michael


***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch