Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

The dynamic alternation building technique described here and exemplified here is good for handling multiple stringA -> stringB replacement, but for an application such as you seem to have, multiple regex to string replacement, it's not a general solution. Here's something that may address your needs more closely. As always, the fine details of regex definition are critical. I still have no idea as to relative speed :)

Win8 Strawberry 5.8.9.5 (32) Sat 10/01/2022 19:44:07 C:\@Work\Perl\monks >perl use strict; use warnings; use Data::Dump qw(dd); # for debug use constant IGNORE => qr{ \A \s* (?: [#] .*)? \z }xms; my $text = <<'TEXT'; Regexes have been tAlKed about as being abstract for 1000's of years. TEXT print "before ---$text--- \n"; my $regex_replacement_string = <<'REGEX'; a 'A' i 'I' # comment line w/o leading spaces e 'E' # optional entry comment ^ [0-9] .*? \s ''# optional comment # comment line with leading spaces (?i) \S*? talk \S* \s ' SPOKEN ' REGEX my ($rx_search, @replacelist) = build_search($regex_replacement_string +); dd $rx_search; # for debug dd \@replacelist; # for debug print "\n"; # for debug $text =~ s{ $rx_search }{$replacelist[$^R]}xmsg; print "after +++$text+++ \n"; sub build_search { my ($rx_replace_string, ) = @_; my $rx_sq_body = qr{ [^\\']* (?: \\. [^\\']*)* }xms; my $rx_comment_to_eol = qr{ [#] .* }xms; my @regexes; my @replacements; use re 'eval'; my @regexlist = split qr{ \s* \n }xms, $rx_replace_string; REGEX_REPLACEMENT: for my $rx_replace (@regexlist) { next REGEX_REPLACEMENT if $rx_replace =~ IGNORE; my $got_rx_replace = my ($rx, $replace) = $rx_replace =~ m{ \A \s* (.*?) \s* # everything before '-pair is regex ' ($rx_sq_body) ' \s* # capture body of '-pair $rx_comment_to_eol? # ignore optional trailing comment \s* \z }xms; die "bad regex/replacement '$rx_replace'" unless $got_rx_repla +ce; my $n = @regexes; $rx = "$rx (?{ $n })"; push @regexes, qr{ $rx }xms; push @replacements, $replace; } # end for REGEX_REPLACEMENT my ($rx_combined) = map qr{ $_ }xms, join ' | ', @regexes; return $rx_combined, @replacements; } # end sub build_search() ^Z before ---Regexes have been tAlKed about as being abstract for 1000's of years. --- qr/ (?msx-i: a (?{ 0 }) ) | (?msx-i: i (?{ 1 }) ) | (?msx-i: e (?{ 2 } +) ) | (?msx-i: ^ [0-9] .*? \s (?{ 3 }) ) | (?msx-i: (?i) \S*? talk \S* \s (?{ 4 }) ) /msx ["A", "I", "E", "", " SPOKEN "] after +++REgExEs hAvE bEEn SPOKEN About As bEIng AbstrAct for of yEArs. +++


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


In reply to Re: Need to speed up many regex substitutions and somehow make them a here-doc list by AnomalousMonk
in thread Need to speed up many regex substitutions and somehow make them a here-doc list by xnous

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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: (3)
As of 2024-04-25 23:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found