tr/[A-Z](w+)/[a-z]$1/ (lowercase the first letter)

Find, replace, find in files, replace in files, regular expressions

tr/[A-Z](w+)/[a-z]$1/ (lowercase the first letter)

Postby dreamwolf » Wed Apr 12, 2006 6:40 pm

I have some Pascal case text ("The QuickTime BrownBear FoxTrot Jumps Over The LazyBone DogMan") that I want to change to Camel case ("the quickTime brownBear foxTrot jumps over the lazyBone dogMan").

In Perl, I'd do something like:

$myvar ~= tr/[A-Z](\w+)/[a-z]$1/;

I'm using PCRE, but I have no idea where I'd put the second have of the tr/// operator....

Any ideas?

Thanks,
dreamwolf
User avatar
dreamwolf
Newbie
 
Posts: 3
Joined: Tue Apr 11, 2006 11:00 pm
Location: Mount Laurel, NJ, USA

Re: tr/[A-Z](w+)/[a-z]$1/ (lowercase the first letter)

Postby Bego » Thu Apr 13, 2006 4:56 am

Hi dreamwolf.

I don't know too.
I tried a find ([A-Z])(\w+)
replace with $1$2
Which "works". It replaces e.g. Jumps with Jumps, so $1 and $2 is "found" correctly. But how to "lowercase" $1 ?
Aint there a new helpfile for perl-regexp-style ?

Hmm, Bego
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: tr/[A-Z](w+)/[a-z]$1/ (lowercase the first letter)

Postby Captain » Thu Apr 13, 2006 5:42 am

Hi

The correct perl syntax would be

my $string = "The QuickTime BrownBear FoxTrot Jumps Over The LazyBone DogMan";

$string =~ s/\b(\w)/\L$1/g;

Unfortunately the UltraEdit Perl Regular expression engine does not recognise the \b (word boundary) character and so this sort of replacement does not work. I will raise a support issue with IDM.

Cheers
User avatar
Captain
Basic User
Basic User
 
Posts: 27
Joined: Tue Jul 26, 2005 11:00 pm

Re: tr/[A-Z](w+)/[a-z]$1/ (How do I lowercase the first lett

Postby Captain » Thu Apr 13, 2006 6:33 am

To do this in UltraEdit should work using the \L (to lowercase) operation.

So you should enter this in the Find What:

\b(\w)

and the following in the replace with:

\L$1

Unfortunately, as I mentioned earlier, UltraEdit has a bug in that it does not recognise the \b character as a word boundary. I have emailed support and hopefully they will deal with this issue. Yet another bug in the Perl regular expression engine.
User avatar
Captain
Basic User
Basic User
 
Posts: 27
Joined: Tue Jul 26, 2005 11:00 pm

Re: tr/[A-Z](w+)/[a-z]$1/ (lowercase the first letter)

Postby Bego » Thu Apr 13, 2006 6:53 am

Or try this:
replace
(\s[A-Z])
with
\L$1

For this example it seems to work, since even a ^ line beginning is interpreted as whitespace :-)

rds Bego
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: tr/[A-Z](w+)/[a-z]$1/ (lowercase the first letter)

Postby dreamwolf » Thu Apr 13, 2006 11:46 am

Thanks for the suggestion, Bego! The very beginning of the string (BOF) didn't get changed for me with that, but I was perfectly willing to modify that one character manually. ;)

Also, piggy-backing on your and Captain's suggestions, I was able to get exactly what I wanted by using \l (lowercase L) instead of \L:
Code: Select all
Find:    ([A-Z])(\w*)
Replace: \l$1$2

"I, The QuickTime BrownBear FoxTrot Jumps Over The LazyBone DogMan"
Becomes:
"i, the quickTime brownBear foxTrot jumps over the lazyBone dogMan"

Woot!

dreamwolf
User avatar
dreamwolf
Newbie
 
Posts: 3
Joined: Tue Apr 11, 2006 11:00 pm
Location: Mount Laurel, NJ, USA

Re: tr/[A-Z](w+)/[a-z]$1/ (lowercase the first letter)

Postby EllE » Mon Jun 19, 2006 1:44 am

and how lowercase last characters without first one ?

example:
Code: Select all
ABCDEF
ABCDEF
ABCDEF

into:
Code: Select all
Abcdef
Abcdef
Abcdef
User avatar
EllE
Basic User
Basic User
 
Posts: 12
Joined: Mon Jun 12, 2006 11:00 pm

Re: tr/[A-Z](w+)/[a-z]$1/ (lowercase the first letter)

Postby dreamwolf » Fri Jul 07, 2006 8:35 pm

EllE wrote:and how lowercase last characters without first one ?

Lowercase "L" ("\l") ensures a single character is lowercase. Uppercase "L" ("\L") capitalizes all letters until it reaches "\E." So, in your case, you can use the principles I have above, but just place a "\L" after the first character. If you also want to be sure the first character is uppercase, use "\u" (same principles apply to "\u" and "\U" as the L's):
Code: Select all
Find:    ([A-Z])(\w*)
Replace: \u$1\L$2

That will change both:
Code: Select all
ABCDEF
ABCDEF
ABCDEF

and
Code: Select all
abcdef
abcdef
abcdef

into:
Code: Select all
Abcdef
Abcdef
Abcdef

Sorry it took a while to respond. I don't cruise these forums very often.

HTH.

dreamwolf
User avatar
dreamwolf
Newbie
 
Posts: 3
Joined: Tue Apr 11, 2006 11:00 pm
Location: Mount Laurel, NJ, USA


Return to Find/Replace/Regular Expressions