There are generally three reasons I'd use a regex.
- To check if a given string has a given pattern that goes beyond substr. Generally, I'll use this in the form next LOOP if /REGEX/;
- To do a substitution that goes beyond substr.
- To grab a substring if I don't know where in the string it starts. This is often quicker and more maintainable than using index and substr.
Unless it's extremely simple, I
never use a regex to parse data in something I ever intend someone (including me) to read in the future. The big thing here is that most programmers will easily understand
unpack or
substr, cause those exist in other C-type languages. However, most programmers will have difficulty understanding most complex regexes.
This isn't to say that a regex shouldn't be used for parsing complex data. Just be aware that someone coming to your code without the benefit of your knowledge may need to be able to maintain it. I'm all for forcing people to learn new stuff, but the learning curve shouldn't be too steep.
------
/me wants to be the brightest bulb in the chandelier!
Vote paco for President!
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.