Re: regexp pattern matching



On Thu, 2006-29-06 at 13:40 -0400, Hair wrote:
Hello, I have a pattern matching question, that I can't seem to find the
answer to either online or in books, so here is the pattern I am trying to
match, and hopefully some kind soul will give me pointers:


The satchel contains the following items (97): item1, item2, etc etc


I can match the first part, but I am trying to extract the item list. I
am having problems getting the pattern to match the (97) [note, that
number varies] and the : and just give me the items in the list.

Yes, this is for a mud, but using perl regex in tinyfugue. I had help on
the tf list getting the majority of this working, but the (97): still gets
added to my list and I don't want it. TIA for any help.

--Tommy Vielkanowitz



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;

Hope this helps.


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


.



Relevant Pages

  • Re: code critique wanted
    ... dew point at pressure level ... This defined the pattern @slice so that aytime it was seen later ... With pointers it's also not ... no deficiencies and the other way is to make it so complicated ...
    (comp.lang.fortran)
  • Re: Setting pointer to null!
    ... debug builds do not and AFAIK have never done that. ... yourself and initialise it NULL on a release build. ... and I think they use a repeated 0xCD pattern. ... if you "defensively" initialize all pointers to ...
    (microsoft.public.vc.language)
  • Re: Collecting Parameter Design Pattern?
    ... Thanks for your great pointers. ... Java and Design Pattern) might not be as amused as I am. So I decided ... understanding pattern within the Java context might be a more appreciated ...
    (comp.lang.java.programmer)
  • Re: Binary operator overloading (returning temporary objects that allocate memory)
    ... define a class SetUnion to hold a list of pointers to Set. ... union of all the sets referred to. ... This pattern can result in massive performance improvements for some ...
    (comp.lang.cpp)
  • Re: regexp pattern matching
    ... and hopefully some kind soul will give me pointers: ... The satchel contains the following items: item1, item2, etc etc ... I am having problems getting the pattern to match the [note, ...
    (perl.beginners)