in reply to regex capture case

As already noted by grizzley, $1 will contain the text from $_ which matched the regex and the text will be in exactly the same form as it appeared in $_. You will need to first extract the matching text with the regex and then carry out any necessary alterations of the match.

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, but the regex itself will not be able to do that for you.

Replies are listed 'Best First'.
Re^2: regex capture case
by rmflow (Beadle) on Jun 23, 2009 at 15:14 UTC
    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.

      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.