Regexp help - upper to lower ONLY matching this pattern

Discussion in 'Perl Misc' started by Patrick, Aug 27, 2003.

  1. Patrick

    Patrick Guest

    Hi,
    I have a bunch of variables that I need changed. Basicly, anywhere i
    see something like this:
    $test{'HI_THERE'}

    I need tranlated to this:
    $test{'hi_there'}

    But I can't use:
    tr/A-Z/a-z/

    because that translates all uppercases to lower cases (and breaks most
    of my code - i.e. $cgi = new CGI; turns into: $cgi = new cgi;).

    Is there anyway to get this to work for that particular patter. I
    tried this, but didn't seem to work (it translated everything to lower
    case):

    perl -pi -e "tr/{'A-Z'}/{'a-z'}/" test.cgi

    Any help would be appriciated. Thanks,
    -Patrick-
    Patrick, Aug 27, 2003
    #1
    1. Advertising

  2. Patrick

    Greg Bacon Guest

    In article <>,
    Patrick <> wrote:

    : I have a bunch of variables that I need changed. Basicly, anywhere i
    : see something like this:
    : $test{'HI_THERE'}
    :
    : I need tranlated to this:
    : $test{'hi_there'}
    :
    : But I can't use:
    : tr/A-Z/a-z/

    You could tighten up your pattern a little, but be aware that this
    isn't a Perl parser and, hence, will fail in the general case:

    s/\{(["']?.*?["']?)\}/{\L$1}/g;

    : [...]

    A great mjd quote applies: "Of course, this is a heuristic, which is a
    fancy way of saying that it doesn't work."

    Greg
    --
    When the government fears the people, there is liberty. When the people
    fear the government, there is tyranny.
    -- Thomas Jefferson
    Greg Bacon, Aug 27, 2003
    #2
    1. Advertising

  3. Patrick wrote:
    >
    > I have a bunch of variables that I need changed. Basicly, anywhere i
    > see something like this:
    > $test{'HI_THERE'}
    >
    > I need tranlated to this:
    > $test{'hi_there'}
    >
    > But I can't use:
    > tr/A-Z/a-z/
    >
    > because that translates all uppercases to lower cases (and breaks most
    > of my code - i.e. $cgi = new CGI; turns into: $cgi = new cgi;).
    >
    > Is there anyway to get this to work for that particular patter. I
    > tried this, but didn't seem to work (it translated everything to lower
    > case):
    >
    > perl -pi -e "tr/{'A-Z'}/{'a-z'}/" test.cgi


    tr/// translates a single character to another character. You probably
    want:

    perl -pi -e "s/({'[[:upper:]]+'})/\L$1/" test.cgi



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Aug 27, 2003
    #3
  4. Patrick <> wrote:

    > anywhere i
    > see something like this:
    > $test{'HI_THERE'}
    >
    > I need tranlated to this:
    > $test{'hi_there'}



    s/\$test{'HI_THERE'}/\$test{'hi_there'}/g;

    That does exactly what you asked for.

    Perhaps you meant to ask for something else?


    Can the hash names be other than "test"?

    Can the hash keys be other than "HI_THERE"? (see below)

    Must it also handle $test{ 'HI_THERE' } and $test{HI_THERE} and $test{"HI_THERE"}?

    How about @test{ 'HI_THERE' }? (a "hash slice")


    This may be closer to what you meant to ask for:

    s/(\$test{')([^']*'})/$1\L$2/xg; # all on one line

    or, written for human consumption:

    s/(
    \$test { ' # hash name, open curly, quote
    )
    (
    [^']* ' } # hash key, quote, close curly
    )
    /$1\L$2/xg;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Aug 27, 2003
    #4
  5. Patrick

    Markus W. Guest

    I am not a regex-expert, but this one works and looks easy.

    perl -p -e "s/{'(.+)'}/{'\L$1'}/" test.cgi

    Markus Weihs


    "Patrick" <> schrieb im Newsbeitrag
    news:...
    > Hi,
    > I have a bunch of variables that I need changed. Basicly, anywhere i
    > see something like this:
    > $test{'HI_THERE'}
    >
    > I need tranlated to this:
    > $test{'hi_there'}
    >
    > But I can't use:
    > tr/A-Z/a-z/
    >
    > because that translates all uppercases to lower cases (and breaks most
    > of my code - i.e. $cgi = new CGI; turns into: $cgi = new cgi;).
    >
    > Is there anyway to get this to work for that particular patter. I
    > tried this, but didn't seem to work (it translated everything to lower
    > case):
    >
    > perl -pi -e "tr/{'A-Z'}/{'a-z'}/" test.cgi
    >
    > Any help would be appriciated. Thanks,
    > -Patrick-
    Markus W., Aug 27, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    696
    J├╝rgen Exner
    Dec 7, 2004
  2. sunil panda

    Lower bound & Upper bound

    sunil panda, Dec 25, 2003, in forum: Java
    Replies:
    9
    Views:
    7,192
    thushara wijeratna
    Oct 7, 2008
  3. ANM
    Replies:
    2
    Views:
    1,339
    Thomas Schodt
    Mar 7, 2004
  4. B Williams
    Replies:
    9
    Views:
    457
    Yannick Tremblay
    Dec 19, 2006
  5. BlackHelicopter
    Replies:
    0
    Views:
    495
    BlackHelicopter
    Jan 31, 2013
Loading...

Share This Page