Ken's Voyages Around the Sun


Regexaissance Man
Previous Entry :: Next Entry

Mood:
Efficient

Read/Post Comments (4)
Share on Facebook
In order to become a better coder, I recently bought Mastering Regular Expressions and have been reading and studying and applying its contents.

Regular expressions -- or regex for short -- are a concise, symbolic, potentially confusing but very powerful language-independent cryptic way to search text for patterns, and also replace the found bits with alternative text.

One of my projects is to fix some code at work. The problem is that student names often come from the central database as Smith-jones, or Stuart Iii, or O'fred where the capitalization is messed up. I'd like to fix it before it goes into our system so hoped this book would help. Indeed, it already has.

My old system had a kludge of about 10-12 lines that tried to deal with the capitalization possibilities. Usually it did not catch everything I wanted it to, but here's a replacement that does. I love regex now!


while ($name =~ m/([ \-']([a-z]|I[iv]+))/g) {
     $uc = uc($1);
     $name =~ s/$1/$uc/;
}


This finds names having a lowercase letter following a space, hypen, or apostrophe and then converts that letter to uppercase. It also converts letters immediately following a leading I if the letters are i or v. That last bit still needs some work in case someone's name is Ivanov or something like that.

Hey, it's geeky, but it excites me! You can do very clever things with regex, indeed.



Read/Post Comments (4)

Previous Entry :: Next Entry

Back to Top

Powered by JournalScape © 2001-2010 JournalScape.com. All rights reserved.
All content rights reserved by the author.
custsupport@journalscape.com