... sometimes there can be "-" character within the string ...
Along with JavaFan, I have no idea what's supposed to happen when a '-' character is encountered, so I'll just tiptoe quietly around that one.
Your apparent first thought about using split doesn't seem so bad:
>perl -wMstrict -le
"my $orig = 'AABBCCDDEEFFGGHH';
my $mod = 'bCCDdEEFfgGH';
;;
my @chunks = split /$mod/i, $orig;
my $new = qq{\L$chunks[0]\E$mod\L$chunks[1]\E};
print qq{'$new'};
"
'aabbCCDdEEFfgGHh'
Update 1: If there might be * + ? [ ] etc. metacharacter-like things in the $mod string, best to split on /\Q$mod\E/i instead.
Update 2: A more general approach (updated again to be a lot simpler):
>perl -wMstrict -le
"my $orig = 'fEEfoobarFIEFooBarFoEfOoBaRfUM';
my $mod = 'FooBar';
;;
my @chunks = split /\Q$mod\E/i, $orig, -1;
my $new = join $mod, map lc(), @chunks;
print qq{'$new'};
"
'feeFooBarfieFooBarfoeFooBarfum'
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|