Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
more useful options
 
PerlMonks  

Preserve Case in Regexp

by stefan k (Curate)
on Mar 16, 2004 at 10:30 UTC ( #336968=perlquestion: print w/ replies, xml ) Need Help??
stefan k has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks,

I just ran into the problem of renaming a C++ class throughout many files. The class-name was in camel case (say TheClass) whereas the filenames were lower case (theclass.cc). The bundle came with a Makefile and naturally I wanted to replace the names in there, too.

The renaming of the files was done using mmv.

And for the contents of the files I wanted to use that favorite perl-one-liner:

perl -pi.bak -e 's/theclass/thenewclass/'
And, yes, I could have done this for every possible case, but I wondered whether all that could be done in one line. After reading man perlre and surfing the Monastery I finally found one solution at Re: Regex: Case insensitive search but case sensitive replace.

But, honestly, this can't be it, can it?

I'd appreciate a "I" modifier (or something like that) for regexeps that means "preserve case" very much like the (X)Emacs variable case-replace does it. It would match case insensitive but perform some magic to the replacement in that it would preserve the case of the matched pattern there.

I am well aware that this would be quite hard for replacements that have a different length from the search pattern. Still, even then the cases at some prominent positions (like the first or the last character or characters after a grouping construct, etc) could be figured out.

Is that possible or already existing, or already planned? Am I missing something here?

In expectation of deeper enlightenment and with many thanks...

Regards... Stefan
you begin bashing the string with a +42 regexp of confusion

Comment on Preserve Case in Regexp
Select or Download Code
Re: Preserve Case in Regexp
by Abigail-II (Bishop) on Mar 16, 2004 at 11:01 UTC
    Is that possible or already existing, or already planned?
    Not for perl5, but feel free to submit a patch.
    Am I missing something here?
    Such a substitution is probably far less common that you might think. Not common enough to use a regex flag and increase the complexity of perl (the interpreter). It would also start endless discussions of what the behaviour would be if the replacement is longer/shorter than the original, and cases where the match is in lower-case, the replacement is in upper-case, and there's no lower-case equivalent for said upper-case. Not to mention the symantics of the /eI combo.

    Abigail

Re: Preserve Case in Regexp
by graff (Chancellor) on Mar 17, 2004 at 03:17 UTC
    Given that perl won't do exactly what you want, perhaps a reasonable compromise is to have it provide a fallback that's easy enough.

    Suppose you use your favorite text editor just to create a list of the mixed-case name replacements -- e.g. the self-respecting unix guru would do something like this:

    $ cat > ClRename.sed s/OldClass/NewClass/g s/RedClass/BlueClass/g s/BadClass/GoodClass/g ... ^D
    Now create a version of the list that includes the downcased names along with the original mixed-case ones:
    perl -pe 'print lc' ClRename.sed > flAndClRename.sed
    The latter output file is now suitable for use as a script (for 'perl -pi.bak' or 'sed -f'). Not the most efficient algorithm, perhaps, but usable.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (12)
As of 2014-04-16 08:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (419 votes), past polls