cthar has asked for the wisdom of the Perl Monks concerning the following question:

I have two sentence

The first one is:
this.is.example.text
and another one is:
[this.is.] [example.text]

Now, i want the first sentence also to be like second sentence. ie, include [ ] in the first sentence. how can i merge the two, so that first sentence also looks like [this.is.] [example.text]

Thanking you in anticipations.

Replies are listed 'Best First'.
Re: Merging in perl
by colwellj (Monk) on Dec 18, 2009 at 05:14 UTC
    Have a look at split and join
    This assumes positioning is always the same.
    Or if it not you can always split by '][', resplit the first item in your split array and see how many items are in it.
    In other words break up your problem into smaller chunks.
Re: Merging in perl
by gmargo (Hermit) on Dec 18, 2009 at 16:58 UTC

    I'm not quite sure what you're asking for. A regular expression for a substitution? Something like this?

    #!/usr/bin/perl -w use strict; use warnings; my $str = "this.is.example.text"; print "Before: $str\n"; $str =~ s/^(\w+\.\w+\.)(\w+\.\w+)$/[$1] [$2]/; print "After : $str\n"; __END__ Before: this.is.example.text After : [this.is.] [example.text]

    If you're looking for something more sophisticated, then you should show us the code you wrote to solve your problem, and explain why that's not working for you.

    If you are looking for something as simple as a regular expression, and I may be wrong about this, but you seem to be asking some very basic questions in your previous threads, then I cannot overstate the usefulness of a good Perl book, like Programming Perl.

Re: Merging in perl
by johngg (Canon) on Dec 18, 2009 at 18:49 UTC

    You can split on the full-stops (which are regular expression metacharacters so need to be escaped) but use capturing parentheses in the regex so that they are retained in the resulting array; (have a look at perlretut, perlreref and perlre). Then you can construct your new string by interpolating array slices (Slices) in a double-quoted string, first having set the list separator (see $LIST_SEPARATOR in perlvar) to an empty string so that each element abuts the next.

    $ perl -E ' > $text = q{this.is.example.text}; > @items = split m{(\.)}, $text; > say for @items; > $new = do > { > local $" = q{}; > qq{[@items[ 0 .. 3 ]] [@items[ 4 .. 6 ]]}; > }; > say $new;' this . is . example . text [this.is.] [example.text] $

    I hope this is helpful.

    Cheers,

    JohnGG

Re: Merging in perl
by $self (Friar) on Dec 18, 2009 at 15:00 UTC
    The way you state the question now, you could just overwrite the first sentence with the second one.
Re: Merging in perl
by fod (Friar) on Dec 19, 2009 at 18:27 UTC

    This will identify bracketed expressions in sentence 2 and bracket them in sentence 1.

    use strict; use warnings; my $snt1 = 'this.is.example.text'; my $snt2 = '[this.is.] [example.text]'; foreach ($snt2 =~ /\[(.+?)\]/g) { $snt1 =~ s/\Q$_\E/[$_]/; } print $snt1;
    (update: removed unnecessary if)