Re: regexp pattern matching



On Thu, 2006-29-06 at 14:31 -0400, Mr. Shawn H. Corey wrote:
OK, here are some pointers:

1. Create a file with lines you want to match. Use copy & paste rather
than generating them by hand; you'll avoid typing errors. Try to include
as many just-barely-hits as you can, that is, those lines you want to
match but are on the boundaries.

The satchel contains the following item (1): item1
The satchel contains the following items (2): item1, item2
The satchel contains the following items (3): item1, item2, item3

2. Create a pattern that has all the common parts and replace the parts
that vary with '.*' Be sure to escape any meta-characters.

m/The satchel contains the following item.* \(.*\): .*/

3. Add parentheses to capture parts.

m/The satchel contains the following item.* \((.*)\): (.*)/

4. Refine the match by replace the '.*' with more specific matches.

m/The satchel contains the following items? \((\d+)\): (.*)/

5. Be sure to record the parts immediately after the match. New matches
will overwrite them.

m/The satchel contains the following items? \((\d+)\): (.*)/
my $count_of_items = $1;
my $item_list = $2;

Oops, forgot:

6. Run the match against the file you created in step 1. Every line
should have output. Verify the output is what it should be.


--
__END__

Just my 0.00000002 million dollars worth,
--- Shawn

"For the things we have to learn before we can do them, we learn by doing them."
Aristotle

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/


.