Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Answer: How do I put bracket on substring(s) of a string?

by jdporter (Canon)
on Sep 10, 2008 at 19:55 UTC ( #710453=categorized answer: print w/replies, xml ) Need Help??

Q&A > strings > How do I put bracket on substring(s) of a string? - Answer contributed by jdporter

sub put_brackets { my( $str, @pats ) = @_; my $mask = ' ' x length $str; my $ofs = 0; for ( @pats ) { $ofs = index $str, $_, $ofs; last if $ofs < 0; # this pattern not found; look for no more. substr $mask, $ofs, length($_), 'a' x length($_); } # print "string= '$str'\n"; # print "mask = '$mask'\n"; join '', map sprintf( /a/ ? '[%s]' : '%s', substr $str, 0, length($_), '' ), split /\b/, $mask }

Log In?

What's my password?
Create A New User
[karlgoethebier]: code><table style="border: 1px solid black;}"></code> same for th and td ?
[shmem]: my scratchpad
[karlgoethebier]: oops!
[shmem]: karlgoethebier: np, got it. But doesn't either :-/
[shmem]: just leaving it as is. After all, enough effort put into that. hrmpf.

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2017-07-24 16:46 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (356 votes). Check out past polls.