As most of you are already aware, Perl has very powerful regular expression support. You can do things with regular expression in perl that cannot be done in any other language. One good example for this is case conversion using regular expression in perl. You can match a string and change its case when you are printing it back. For example you can convert 'case conversion using regular expressions in perl' to 'Case Conversion Using Regular Expressions In Perl' using just a regular expression.
I recently had the need for this when I was converting one of my old sites, BinnyVA, from pure HTML site to a site with PHP backend. I also wanted to make the code valid when I was converting it - and this involved make the tags and attributes lowercase. For this job, I turned to an old trusted friend of mine - Perl.
I have never used this particular feature of regular expression before - so I had to search for some time to find the answer. Unfortunately, I did not find any articles on this topic - the best I could find was a small reference within a tutorial about perl. So, I am creating a post for this topic - to aid future searchers in the same quest.
As an example, let us take this sentence.
the baby's blood type? human, mostly.
We want to convert it to title case ie.
The Baby's Blood Type? Human, Mostly.
The regular expression to do this is...
This is what it does...
s/ (\b) #Get word's preceding char must be a word boundary(\b) ([a-z]) #Get the first lowercase letter after the word boundary / #The Replacements... \1 #Put the word boundary back in. \u\2 #'\u' uppercases the next character /g;
The full program looks like this...
#!/usr/bin/perl $_ = 'the baby\'s blood type? human, mostly.'; s/(\b)([a-z])/\1\u\2/g; print;
computer, did we bring batteries? computer?
COMPUTER! Did we bring batteries? computer?
We can do this using the regexp...s
(computer), ([a-z])/\U\1\E! \u\2
See the part
\U\1\E? This will uppercase every character from the
\U escape to
\E. In this case, It uppercases the full word 'COMPUTER'.
You can use the following escape sequences to change the case.
- \l (Small L)
- Lowercase next character
- Uppercase next character
- Lowercase until
- Uppercase until
- End case modification
I tired to do in in other languages like PHP and found it impossible. What about your favorite language? Can you convert the case of a string using just a regular expression?