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 ); }


In reply to Re: Regexp help!! by Util
in thread Regexp help!! by kanish

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.