this is a very confusing question. do you want to turn "my name is George Bush and I live in America" into "my name is <name> and I live in <country>" or the other way around?

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

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"; }
produces:
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>
if you are trying to go the other way, then consider one of the many templating packages available..

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


In reply to Re: Replacing Types by mreece
in thread Replacing Types by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.