Re: match string by re using some pattern
- From: Martien verbruggen <mgjv@xxxxxxxxxxxxxxxxxx>
- Date: Thu, 26 Jul 2007 13:46:51 +1000
On Thu, 26 Jul 2007 02:21:42 -0000,
frytaz@xxxxxxxxx <frytaz@xxxxxxxxx> wrote:
On Jul 25, 11:26 pm, anno4...@xxxxxxxxxxxxxxxxxxxxxx wrote:
fry...@xxxxxxxxx <fry...@xxxxxxxxx> wrote in comp.lang.perl.misc:
[huge snip]
script will work fine only for 1st file line
#one# should match O!N!E for instance
[snip]
I am still at a loss guessing what you are really up to.
OK, I'll try to explain it
for instance we parse http web page
$line = "section BOOKS - title SOME_BOOK_TITLE - price 20";
$pattern = "section #SECTION# * title #TITLE# - price #PRICE#";
$regex_patern = "section (.+?) . title (.+?) - price (.+?)";
if ($line =~ m/^$regex_pattern/) {
$section = $1;
#$section -> BOOKS
$title = $2;
#$title -> SOME_BOOK_TITLE
$price = $3;
#$price -> 20
}
You should really consider using real code. Something that compiles and
runs, under strict and warnings.
The above doesn't.
Allowing that maybe $regex_patern and $regex_pattern are suppose dto be
the same, what is $pattern doing in there? It isn't a pattern as far as
Perl is concerned, and it's actually not used anywhere.
and its all fine because its in order section,title,price
now we try to parse other page where
$line = "title CD_TITLE - price 50 - section MUSIC";
$pattern = "title #TITLE# - price #PRICE# - section #SECTION#";
$regex_pattern = "title (.+?) - price (.+?) - section (.+?)";
if ($line =~ m/^$regex_pattern/) {
#now its wrong
$section = $1;
#$section -> CD_TITLE
$title = $2;
#$title -> 50
$price = $3;
#$price -> MUSIC
}
in this example, need to put different order of section,title,price
I want to make script know pattern when replacing #TITLE# make it a
title match pattern, but its in all cases (.+?)
It's still completely unlcear to me what you want, and what $pattern has
to do with it. The only thing i can partly parse of the above is that
you're trying to get the three fields title, price and section from a
string, and that they could be in any of the 'slots' in $line. One way
that would work is something like:
#!/usr/bin/perl
use strict;
use warnings;
my @lines = (
"section BOOKS - title SOME_BOOK_TITLE - price 20",
"title CD_TITLE - price 50 - section MUSIC",
"price 3 - title BARF BANANA - section RANDOM",
);
my $kv_pattern = qr/(title|price|section) (.+?)/;
for my $line (@lines)
{
print "$line\n";
my %kv = $line =~ /$kv_pattern - $kv_pattern - $kv_pattern/;
while (my ($key, $value) = each %kv)
{
print "\t$key -> $value\n";
}
}
As you can see, after a successful match, the has %kv contains the keys
and values as stored in the line you put in. Simply access them as
$kv{section}, $kv{price} and $kv{title}. This still requires a fairly
rigorous input, but allows the order to be different, which SEEMS to be
what you're asking, although I'm not 100% certain.
If you need to support more or different fields, simply change
$kv_pattern.
Martien
--
|
Martien Verbruggen | That's not a lie, it's a terminological
| inexactitude.
|
.
- Follow-Ups:
- Re: match string by re using some pattern
- From: Gunnar Hjalmarsson
- Re: match string by re using some pattern
- References:
- match string by re using some pattern
- From: frytaz@xxxxxxxxx
- Re: match string by re using some pattern
- From: frytaz@xxxxxxxxx
- Re: match string by re using some pattern
- From: Paul Lalli
- Re: match string by re using some pattern
- From: frytaz@xxxxxxxxx
- Re: match string by re using some pattern
- From: anno4000
- Re: match string by re using some pattern
- From: frytaz@xxxxxxxxx
- match string by re using some pattern
- Prev by Date: Re: @arts
- Next by Date: Re: @arts
- Previous by thread: Re: match string by re using some pattern
- Next by thread: Re: match string by re using some pattern
- Index(es):
Relevant Pages
|