Home > Archive > PERL Beginners > June 2006 > regexp pattern matching
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
regexp pattern matching
|
|
|
| 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
| |
| Dr.Ruud 2006-06-29, 6:58 pm |
| Hair schreef:
> 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.
You're problem might be that you use the () unescaped.
#!/usr/bin/perl
use strict ;
use warnings ;
$\ = $, = $" = "\n" ; # see perldoc perlvar
$_ = 'The satchel contains the following items (97):
item1, item2, etc' ;
my $re = qr/^.*?\((\d+)\):\s*(.*)/ ;
my ($count, $csv) = /$re/ ;
if ($count)
{
print $count, $csv ;
@_ = split /,[[:blank:]]*/, $csv ;
print '', scalar @_, @_ ;
}
--
Affijn, Ruud
"Gewoon is een tijger."
| |
| Dr.Ruud 2006-06-29, 6:58 pm |
| "Dr.Ruud" schreef:
> You're problem might be that you use the () unescaped.
Hihi, that's what you get when you shift from
You're probably ...
to
Your problem ...
mid-sentence.
--
Affijn, Ruud
"Gewoon is een tijger."
| |
| Mr. Shawn H. Corey 2006-06-29, 6:58 pm |
| 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/
| |
| Mr. Shawn H. Corey 2006-06-29, 6:58 pm |
| 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/
| |
| M. Kristall 2006-06-30, 6:57 pm |
| Mr. Shawn H. Corey wrote:
> m/The satchel contains the following items? \((\d+)\): (.*)/
> my $count_of_items = $1;
> my $item_list = $2;
And don't forget to check if it matched (otherwise you have the old $1
and $2):
if (m/The satchel contains the following items? \((\d+)\): (.*)/) {
my $count_of_items = $1;
my $item_list = $2;
# ...
}
|
|
|
|
|