gopalr has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I use the below to find the Text in Ms-Word File by using OLE.

use OLE; use Win32::OLE::Const 'Microsoft Word'; my $const=Win32::OLE::Const->Load("Microsoft Word 9.0 Object Library") +; $word=GetObject OLE("Word.Application"); $word->Selection->Find->{'MatchWildcards'}=True; $word->Selection->Find->{'Text'}='\<web\>*\</web\>'; if ($word->Selection->Find->Execute) { ## $prematch=(15 characters only) ## $postmatch=(15 characters only) }

But I need to get the Pre-Match & Post-Match characters from Selection Text.

Pre-Match & Post-Match length only should be 15 characters.

Input File

This is Sample Pre Match Text<web>www.hotmail.com</web>This is Sample +Post Match Text

The following variables should contain like this

$prematch=' Pre Match Text'; $postmatch='This is Sample ';

Pls. help me how to get the length.

Thanks

Gopal.R

Replies are listed 'Best First'.
Re: How to get the Pre-Match and Post-Match
by CountZero (Bishop) on Feb 07, 2005 at 07:01 UTC
    Look for a function in MSWord to extend the selection 15 characters forward and backward, then grab the so extended selection into Perl and extract (using substr the first 15 and last 15 characters.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: How to get the Pre-Match and Post-Match through Win32::OLE
by gube (Parson) on Feb 07, 2005 at 07:03 UTC

    Hi, gopal try this

    use Win32::OLE; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Word'; $word=Win32::OLE->GetActiveObject('Word.Application'); $doc= $word->activedocument; $rng = $doc->{range}; $rng->find->clearformatting; $rng->find->replacement->clearformatting; $rng->select; $str = $rng->text; ($prematch,$postmatch)=$str =~ m#(.*?)<web>.*</web>(.*)#gsi; ($pre) = $prematch =~ m#(.{15})#gsi; ($post) = $postmatch =~ m#(.{15})#gsi; print "\n$pre"; print "\n$post"

    Regards,
    Gube
Re: How to get the Pre-Match and Post-Match through Win32::OLE
by prasadbabu (Prior) on Feb 07, 2005 at 07:02 UTC

    gopalr u can try the following or u can use substr,

    use Win32::OLE; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Word'; $word=Win32::OLE->GetActiveObject('Word.Application'); $doc= $word->activedocument; $rng = $doc->{range}; $rng->select; $a = $rng->text; $a='This is Sample Pre Match Text<web>www.hotmail.com</web>This is Sam +ple Post Match Text'; $a =~ m|<web>www\.hotmail\.com<\/web>|si; $pre= $`;$post= $'; ($pretext)= $pre =~ /(.{15})$/i; ($posttext)= $pre =~ /^(.{15})/i; print "pre:$pre\npost:$post\n"; print "\n\n"; print "pre:$pretext\npost:$posttext\n";

    Prasad

Re: How to get the Pre-Match and Post-Match through Win32::OLE
by holli (Abbot) on Feb 07, 2005 at 08:57 UTC
    prasadbabu uses $pre= $`;$post= $'; that is bad for the performance for your script, because it slows down the regex-engine.
    $gubes code can be optimized, no need for unpack():
    $str='This is Sample Pre Match Text<web>www.hotmail.com</web>This is S +ample Post Match Text'; ($pre,$post)=$str =~ m#(.{1,15})<web>.*</web>(.{1,15})#gsi; print "\n$pre"; print "\n$post"


    holli, /regexed monk/