in reply to Re: regex capture case
in thread regex capture case

If you tell us the rules used to determine what alterations need to be made to $1, then we may be able to suggest the most efficient ways of accomplishing that

The objective is to make some rules for formatting of certain texts, for example:
rule: PowerGenerator\d+ text: pOwerGeNERator53

the text should be transformed to PowerGenerator53

other example:
rule: Data\d+Bus_[ABC]\d+ text: DATA5Bus_b3

should be converted to Data5Bus_B3

If the text does not match to rule then no changes should be done.

Replies are listed 'Best First'.
Re^3: regex capture case
by johngg (Canon) on Jun 23, 2009 at 17:09 UTC

    Given your rules, this code seems to do what you want but it uses a string eval, the use of which should be treated with caution.

    use strict; use warnings; my @phrases = ( q{Supply from pOwerGeNERator53 today.}, q{DATA5Bus_C3 routed via PoweRgeNerator71 to data17buS_a3}, q{The newPowerGenErATor6 will not change}, ); my %rules = ( q{(?i)\bpowergenerator(\d+)\b} => q{qq{PowerGenerator$1}}, q{(?i)\bdata(\d+)bus_([ABC])(\d+)\b} => q{qq{@{ [ qq{Data$1Bus_} . uc $2 . $3 ] }}}, ); foreach my $phrase ( @phrases ) { print qq{Original: $phrase\n}; my $newPhrase = $phrase; foreach my $rule ( keys %rules ) { $newPhrase =~ s{$rule}{ eval $rules{ $rule } }eg; } print qq{ Amended: $newPhrase\n\n}; }

    The output.

    Original: Supply from pOwerGeNERator53 today. Amended: Supply from PowerGenerator53 today. Original: DATA5Bus_C3 routed via PoweRgeNerator71 to data17buS_a3 Amended: Data5Bus_C3 routed via PowerGenerator71 to Data17Bus_A3 Original: The newPowerGenErATor6 will not change Amended: The newPowerGenErATor6 will not change

    I hope this is of interest.

    Cheers,

    JohnGG

      This was indeed helpful, thank you.