Friendly Monks,

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-t

Edited by castaway, added code tags around latex examples.


In reply to Changing case inside substitution by skillet-thief

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.