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


In reply to Seduced by a Regex by kelan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.