in reply to Regexp help!!

PPI can do most of the work for you. The code below illustrates how to use PPI to extract the find and replace patterns.

Note: The current PPI, version 1.103, has a bug that I have worked around in my code. I am submitting a patch to fix the bug; when a version is released without the bug, I will post an update with the new version number.

Working, tested code:

use strict; use warnings; use PPI; my $source_code = <<'END_OF_SOURCE_CODE'; $text =~ s#Find \#Text#Replace Text#; $text =~ s/Find \/Text/Replace Text/; $text =~ s{Find \}Text}{Replace Text}; $text =~ s{Find \}Text} {Replace Text}; $text =~ s[(Find) (\}Text)] <Replace Text>io; END_OF_SOURCE_CODE my $Document = PPI::Document->new(\$source_code) or die; my $subs = $Document->find('PPI::Token::Regexp::Substitute'); #use Data::Dumper; $Data::Dumper::Useqq = 1; #print Dumper $subs; #exit; foreach ( @{ $subs } ) { my $type = $_->{'braced'} ? 'Braced' : 'Not Braced'; my ( $find_text, $repl_text ) = extract_subst_strings($_); print "\n$type\n"; print "find_text = '$find_text'\n"; print "repl_text = '$repl_text'\n"; } # Fails on braced substitute statements in PPI 1.103 #sub extract_subst_strings { # my %s = %{ $_[0] }; # # return map { # substr( $s{'content'}, $_->{'position'}, $_->{'size'} ) # } @{ $s{'sections'} }; #} sub extract_subst_strings { my ($substitute_statement) = @_; my %s = %$substitute_statement; warn unless $s{'_sections'} == 2; warn unless @{ $s{ 'sections'} } == 2; my $content = $s{'content' }; my @sections = @{ $s{'sections'} }; my ( $find_text, $repl_text ); if ( $s{'braced'} ) { $find_text = substr( $content, $sections[0]{'position'}, $sections[0]{'size'}, ); $repl_text = substr( $content, $sections[1]{'position'} - $sections[1]{'size'} - 1, $sections[1]{'size'}, ); } else { ( $find_text, $repl_text ) = map { substr( $content, $_->{'position'}, $_->{'size'} ) } @sections; } return ( $find_text, $repl_text ); }