Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: [RFC] [OT] Building Regex Alternations Dynamically (updated)

by AnomalousMonk (Archbishop)
on Jan 19, 2017 at 20:34 UTC ( [id://1179940]=note: print w/replies, xml ) Need Help??


in reply to Re^2: [RFC] Building Regex Alternations Dynamically (updated x2)
in thread Building Regex Alternations Dynamically

(The following is perhaps a bit OT to the main thread, or else already touched upon in an update. Oh, well...)

... I think that adding an extra (?:...) makes the generated regex look a little more complex than it needs to be ... those are just stylistic concerns ...

Please see Re: Recognizing 3 and 4 digit number and thereunder for a long discussion between myself and kcott on these "stylistic concerns." Personally, I still don't see the need for the extra explicit  (?:...) wrap step. The implicit wrap becomes explicit quick enough if you print the stringized Regexp object, and this feature of a Regexp object should be deeply understood from the moment one begins to use them.

... skip the intermediate string variable, like in Haarg's post here: my ($regex) = map {qr/$_/} join '|', map .... ... it's more robust ...

To continue the previous point, I feel it's important to get a regex into a Regexp form as quickly as possible: no dilly-dallying. Once objectified, it can be used atomically when composing more complex regex expressions:

my $rx = qr{ ... }xms; ... $string =~ m{ $rx* $rx+? $rx{3,4} }xms and do_something();
(Of course, this compositional capability is also addressed by the  (DEFINE) predicate of the  (?(condition)yes-pattern) conditional expression of Perl 5.10+.)

The only situation which I'm aware of in which this compositional atomicity breaks down is for something like

my ($n, $m) = (3, 4); ... $string =~ m{ $rx{$n,$m} }xms and do_something();
where  $rx{$n} $rx{$n,} $rx{$n,$m} are all taken as hash element accesses. This can be fixed simply by an explicit layer of non-capturing group wrapping (entirely necessary here!):
    (?:$rx){$n} (?:$rx){$n,} (?:$rx){$n,$m}


Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1179940]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-03-28 14:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found