Regexp help - upper to lower ONLY matching this pattern

P

Patrick

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

Greg Bacon

: 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
 
J

John W. Krahn

Patrick said:
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
 
T

Tad McClellan

Patrick said:
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;
 
M

Markus W.

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

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

Markus Weihs
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top