"be consistent" | |
PerlMonks |
Seduced by a Regexby kelan (Deacon) |
on Sep 30, 2002 at 15:30 UTC ( [id://201743]=perlmeditation: print w/replies, xml ) | Need Help?? |
Or: How I Forgot (and Remembered) Normal String Processing
Remember the days of doing string processing in C? The old goodies like strlen, strchr, strcmp, strcpy, and their relatives, were close at hand. Of course dynamically typed languages such as Perl have usually had nicer string manipulation capabilities; indeed, that being a much heralded advantage to these languages. It is an opinion with which I happen to agree, but I was to learn that some things can be too good. Perl has a delicious extension to string manipulation: regular expressions.1 At first when I began learning Perl I thought they were okay but didn't really understand how to build good ones. As I became more familiar with the concept and Perl's regex syntax, I became ever more enamored with them. These regular expressions are great! With their help, I can do anything involving strings! Matching, substitution, pulling out embedded info, and hundreds of other uses all came easily within my reach. My shiny new toy was pulled out on every possible occassion. 'Hey, that can be done with a regex!' could have been my mantra. But love had blinded me. I had forgotten that can does not always mean should. I have recently had this realization, and it was spurred by taking more notice of certain answers given for questions regarding regex problems. Many times, the asker's problem can be solved with a simple substr or even index; more and more, I have been appreciating the replies that offer those solutions. And I have begun to catch myself when considering using a regex, to think if there is a simpler way with the other tools at my disposal. So Regex Lovers, listen up! I'm not trying to tell you to eschew these wonderous doodads. But do not forget the old fashioned and sometimes simpler ways. substr, (r)index, reverse can be powerful tools. Remember them, use them. And thank you to those who have helped to remind me. kelan 1. Yes, I know Perl is not the only language with regular expressions. But most of the others have them as a direct consequence of their popularity in Perl.2 2. I also don't know much language history so please forgive me if your-favorite-language-that-has-regexs had them before or independent of Perl. Yak it up with Fullscreen ChatterBox
Back to
Meditations
|
|