3 ms·
Regular expressions are just like everything in programming. Used properly they are a huge boon. Used improperly, they're a disaster. Here's a practical exam
by colomon 14y ago
Regular expressions are just like everything in programming. Used properly they are a huge boon. Used improperly, they're a disaster.
Here's a practical example. I spent half a day coding this algorithm (ASCII string in STEP format to an array of Unicode code points) in 70 lines of C++ without regular expression. It took me 15 minutes to recode it in Perl 6 using regular expressions:
while $step.chars > 0 {
given $step {
when /^ '\\S\\' (.)/ {
@unicode.push(ISO8859toUnicode($page, 128 | $0.ord ));
}
when /^ '\\P' (.) '\\'/ {
$page = $0.ord - 'A'.ord + 1;
}
when /^ '\\X' [ '2' | '4' ] '\\' (<hexdigits>+) '\\X0\\'/ {
@unicode.push(:16($0));
}
when /^ (.)/ {
@unicode.push($0.ord);
}
}
$step = $/.postmatch;
}
This (admittedly completely untested) version is drastically shorter and much less brittle.