regex question

G

googler

Hi all,

Say I have a variable $str as below.
$str = "a_long_object_name";

I want to transform $str to something like "aLongObjectName". How can
this be done using regular expression? There can be any number of
constituent words in the original string, each of them separated by an
underscore character.

Thank you.
 
A

attn.steven.kuo

googler said:
Hi all,

Say I have a variable $str as below.
$str = "a_long_object_name";

I want to transform $str to something like "aLongObjectName". How can
this be done using regular expression? There can be any number of
constituent words in the original string, each of them separated by an
underscore character.


$str =~ s/_(\w+)/\U$1/g;

print $str;


See 'perldoc perlre' for details.
 
B

Bill Segraves

googler wrote: ....


$str =~ s/_(\w+)/\U$1/g;

print $str;


See 'perldoc perlre' for details.

Note to OP: The above (proposed) solution produces the result
aLONG_OBJECT_NAME, which is not the specified result. A correct
solution, with explanation of why the above approach fails, was given
in another response to the OP's post.

Use of the appropriate character class, i.e., [a-z], and the
appropriate escape, i.e., \u, fixes the problem, e.g.,

$str = "a_long_object_name";
$str =~ s/_([a-z])/\u$1/g;
print $str;

produces aLongObjectName as the result.

Note: With the OP's specification, the "+" qualifier on the [a-z]
character class is superfluous, as we are asked only to change the
first character of each word that follows a "_" character to upper
case.

Cheers,
 
B

Bill Segraves

Bill said:
(e-mail address removed) wrote: ....
$str =~ s/_(\w+)/\U$1/g;
....
....
Use of the appropriate character class, i.e., [a-z], and the
appropriate escape, i.e., \u, fixes the problem, e.g.,

$str = "a_long_object_name";
$str =~ s/_([a-z])/\u$1/g;
print $str;

produces aLongObjectName as the result.

Note: With the OP's specification, the "+" qualifier on the [a-z]
character class is superfluous, as we are asked only to change the
first character of each word that follows a "_" character to upper
case.

Note 2: If the "+" qualifier is used on the [a-z] character class, \u
is the correct escape. OTOH, if the "+" qualifier is not used (e.g.,
see above), then the form of the escape, \u or \U, is irrelevant, i.e.,
produces the same result. Mea culpa! The \u in this author's solution
was an artifact from the solution in a previous post.

So, in conclusion, the minimum change to make Steven's solution work
correctly is to change the character class and qualifier from \w+ to
[a-z].

Cheers,
 
A

Arved Sandstrom

[ SNIP ]
Note to OP: The above (proposed) solution produces the result
aLONG_OBJECT_NAME, which is not the specified result. A correct
solution, with explanation of why the above approach fails, was given
in another response to the OP's post.
[ SNIP ]

Worth pointing out that a test taking perhaps a minute would have sufficed
to show that the

$str =~ s/_(\w+)/\U$1/g;

doesn't do the trick. Your solution follows. The point being, if one posts a
code snippet that one purports to work, one should actually test it.

AHS
 
A

Arved Sandstrom

Bill Segraves said:
Thanks, Arved. Your are indeed correct.

Personally, I was reluctant to point out to the respondent, Steven Kuo,
what
I felt should have been obvious to him, as well as to the OP, after
reading
my responses. Thanks to your suggestion, googler and Steven Kuo needn't
have
to infer, from what I wrote, the point that you stated explicitly.

Never hurts to be explicit. :) I'm not trying to bust on anyone by any
means. I have committed the mistake numerous times of trotting out code that
was supposed to work but didn't, that I had never tested, and it's been an
embarrassment for me, possibly a timewaster or aggravation for the
questioner, and all-round just doubleplusungood. At least on Usenet or
mailing lists now, I test everything.
BTW, I tested all of the variations that were mentioned, before copying &
pasting the results into my postings.

I personally had no doubts that you had done so.

The thing is, it's perfectly OK to suggest untested code - just say that
it's untested. For some problems I wouldn't even expect a person offering
advice to actually fire up all the software - I might have something to say
about coding an EJB in a J2EE app, but I may not wish to spend 10 or 15
minutes writing and deploying an application to test a one-line suggestion.
For a simple Perl regex, though, it's hard to think of a reason why you
couldn't knock out the test in under a minute.

AHS
 

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
474,202
Messages
2,571,055
Members
47,659
Latest member
salragu

Latest Threads

Top