Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Regex + substring

by AnomalousMonk (Abbot)
on Mar 27, 2012 at 14:36 UTC ( #961941=note: print w/ replies, xml ) Need Help??


in reply to Regex + substring

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


Comment on Re: Regex + substring
Select or Download Code
Re^2: Regex + substring
by Anonymous Monk on Mar 27, 2012 at 16:10 UTC

    Sorry I just coppied the the code without looking at the text. When "-" is encounter the output would have to be the same with just the - included i.e.

    $O = "AAABBBCCCDDD"; $Mod = "BbBCc-C"; $Result = "aaaBbBCc-Cddd";

      Maybe:

      >perl -wMstrict -le "my $orig = 'fEEfoobarFIEFOOBARFoEfOoBaRfUM'; my $mod = 'Foo-Bar'; ;; (my $modmod = $mod) =~ tr/-//d; my $new = join $mod, map lc(), split /\Q$modmod\E/i, $orig, -1; print qq{'$new'}; " 'feeFoo-BarfieFoo-BarfoeFoo-Barfum'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://961941]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (11)
As of 2014-10-23 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (124 votes), past polls