Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Regex + substring

by Anonymous Monk
on Mar 27, 2012 at 11:15 UTC ( #961896=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear wise Monks,

I have an original string and a modified one;

$Original = "AABBCCDDEEFFGGHH" $Modified = "bCCDdEEFfgGH"

The modified is a substring of original with lower capital letters at some random position. What I would like to do is to find the position of the Modified string within the Original and append the flanking characters that are missing from the modified (in lower letter). The small letters in Modified should stay as they are and sometimes there can be "-" character within the string (like "AABB-AA"). So the output;

$Modified = "aabbCCDdEEFfgGHh"

I thought about using regex than split using this regex then using soft referencing but I think that's overdoing it. BTW what would be the best perl functions to learn to help me with this sort of task in the future? Thanks all for help.

Comment on Regex + substring
Select or Download Code
Re: Regex + substring
by JavaFan (Canon) on Mar 27, 2012 at 11:19 UTC
    I'd lowercase both strings, use index to find where the second one matches in the first, and then it's a matter of using substr (and length) to get the missing parts.

    However, I do not understand the purpose of the "-" character. You make a point of telling that this character can be present in "the string" (which one? either?), but don't tell us whether this character has any special meaning.

Re: Regex + substring
by jwkrahn (Monsignor) on Mar 27, 2012 at 11:41 UTC
    $ perl -le' my $Original = "AABBCCDDEEFFGGHH"; my $Modified = "bCCDdEEFfgGH"; print $Modified; ( $Modified, my $temp ) = ( $Original, $Modified ); $Modified =~ s/(.*?)(?i:$temp)(.*)/\L$1\E$temp\L$2\E/; print $Modified; ' bCCDdEEFfgGH aabbCCDdEEFfgGHh
Re: Regex + substring
by moritz (Cardinal) on Mar 27, 2012 at 11:49 UTC
    BTW what would be the best perl functions to learn to help me with this sort of task in the future?

    perlfunc has a section "Functions for SCALARs or strings". You should get a feeling for what each function does.

    But of course they are only a toolbox, you have to learn how to use them effectively. That's not something that the perldocs teach you, it's what you learn from general programming books, books on algorithms, cookbooks, experience and from reading other people's code.

      Thanks to both of you guys. I will have a look at this perldoc section.
Re: Regex + substring
by AnomalousMonk (Abbot) on Mar 27, 2012 at 14:36 UTC
    ... 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'

      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: perlquestion [id://961896]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2014-12-27 21:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls