in reply to Replacing Types
assuming the former, one way would be to first build up a regular expression for each 'type' you want to discover, then loop through them and perform substitutions on your sentence. consider something along these lines..
produces:use strict; use warnings; ## some sample 'types' and potential values my %types = ( name => [ 'George Bush', 'Osama Bin Laden', ], country => [ 'America', 'Your Pants', ], ); ## turn those arrays into foo|bar|baz (quotemeta to deal with potentia +l U.S.A.) my %regexes = map { $_ => join( '|', map quotemeta, @{ $types{$_} }) } + keys %types; ## some sample sentences my @sentences = ( 'my name is George Bush and I live in America', 'my name is Osama Bin Laden and I want to visit Your Pants', ); ## discover 'types' in each sentence foreach my $sentence ( @sentences ) { print "before: $sentence\n"; while ( my ($type, $pattern) = each %regexes ) { $sentence =~ s/$pattern/<$type>/g; } print " after: $sentence\n\n"; }
if you are trying to go the other way, then consider one of the many templating packages available..before: my name is George Bush and I live in America after: my name is <name> and I live in <country> before: my name is Osama Bin Laden and I want to visit Your Pants after: my name is <name> and I want to visit <country>
(update: and yeah, if you don't have a pre-defined list of names and countries you are searching for, this is a much different problem, as ayrnieu references above.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Replacing Types
by Anonymous Monk on Sep 21, 2006 at 19:12 UTC | |
by mreece (Friar) on Sep 21, 2006 at 20:26 UTC |