skillet-thief has asked for the wisdom of the Perl Monks concerning the following question:
I am having trouble with something I thought would be easy with perl regexes, but though I have been learning lots about perl recently, I have come to you with my first request ever for wisdom.
I have large text files (latex, actually) with commands that look like this: \cite[page 10]{dickens}. Sometimes, however, it might be \cite[page 10]{Dickens}. So I wanted a regex to change Dickens to dickens or Steinbeck to steinbeck, that give me a lower case first letter. (Sorry, there are supposed to be square brackets around "page 10".)
However, none of the simple methods (simple enough for me to know about) seem to work. The first part of the regex is easy:
s{( \\cite\[[^]]\]\{ ([A-Z]) ) }
But I'm not sure what to put in the second part. I've tried doing
{$1\l$2}gxe;
but that doesn't seem to work. So then I tried doing some things with (?{ }) -- using the /e operator, including stuff like this:
sub lower{ my $letter = shift; return lc($letter); } s{...snip...}{ $1 (?{ lower( $2 ) } ) }gxe;
But by this time I started thinking that I was getting way too creative and that there must be a simpler solution.
So beyond figuring out a solution to my problem, I would also be curious to know what is wrong with my understanding of the (?{ }) construction in substitutions.
Many thanks
Monkily yours,
s-tEdited by castaway, added code tags around latex examples.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Changing case inside substitution
by PodMaster (Abbot) on Sep 27, 2003 at 14:13 UTC | |
by skillet-thief (Friar) on Sep 27, 2003 at 14:33 UTC | |
|
Re: Changing case inside substitution
by gjb (Vicar) on Sep 27, 2003 at 14:12 UTC | |
|
Re: Changing case inside substitution
by Not_a_Number (Prior) on Sep 27, 2003 at 14:40 UTC | |
|
Re: Changing case inside substitution
by tachyon (Chancellor) on Sep 27, 2003 at 14:13 UTC | |
|
Re: Changing case inside substitution
by skillet-thief (Friar) on Sep 27, 2003 at 15:41 UTC |