in reply to Regular Expressions: Call for Examples
Not simple dinky ones, but perhaps regexes that got you out of a bind, or were quite sneaky at what they did, ...
A couple of times recently I've used a "nested" regexp to pull off a bit of tricky substition. The "outer" regexp serves as a filter, and the "inner" regex, fired via /e, does a more targetted subsitution (or no substitution at all).
In the code below, the challenge was to turn words like "cowsCanFly" into "cows-can-fly".
When I first posted this fragment (in this node), there was some concern that the regex engine wasn't reentrant, and that I'd just gotten lucky. Perhaps, though I've done this a few times with 5.6.0 or later, and haven't run into any problems.$phrase = "NoMatch cowsCanFly sheepAreVeryCool NoMatch"; $phrase =~ s{ \b ( [a-z]+ (?:[A-Z][a-z]+)+ ) \b }{ my $word = $1; $word =~ s/([A-Z])/"-" . lc($1)/eg; $word; }gex; print $phrase, "\n";
japhy, since you're now a regex UberLord, perhaps you can vet this approach for reentrancy issues.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Regular Expressions: Call for Examples
by japhy (Canon) on Jul 22, 2002 at 14:39 UTC | |
by Abigail-II (Bishop) on Jul 22, 2002 at 14:49 UTC | |
by japhy (Canon) on Jul 22, 2002 at 14:52 UTC |
In Section
Meditations