Re: match string by re using some pattern



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.
|
.



Relevant Pages

  • Re: Chord connection
    ... i.e. Accents don't come from meter but meter ... Maybe I need a better definition of accent. ... Pattern Accent - a stress created by a repetitive pattern (making the first ...
    (rec.music.theory)
  • Re: Why float is called as float, not real?
    ... Lane Straatman wrote: ... to prove mathematical theorems? ... Perhaps we mean different things by "pattern". ...
    (comp.lang.fortran)
  • Re: The Case for a Designer
    ... There must have been guidance - i.e. intelligent intervention - for this to ... I don't see how a rapid change in head size is more impressive than ... There is a pattern here. ...
    (talk.origins)
  • Re: How much to charge for swim suits & corsets
    ... I've made swim suits from patterns before. ... So, rough estimate, I'm going to say 2 hours to draft the pattern. ... If your clients came to me for the same thing you've been doing for them, ... The price is the price. ...
    (alt.sewing)
  • Re: Historical Currency Trends
    ... The equilibrium is between the number of buyers and the price ... they will take versus the sellers and the price they will take. ... that pattern will repeat in future. ...
    (uk.finance)