http://qs1969.pair.com?node_id=496464


in reply to Re: Regular Expression problem when Extracting Start\ VALUE \End
in thread Regular Expression problem when Extracting Start\ VALUE \End

I got an error when tried to use Text::Balanced I verified that I have Balanced.pm under /lib/Text Thanks
use Text::Balanced; $text='blabla<Else><LogEntry message="FAIL TESTCASE "/><FailTestCase/> +</Else>blabla'; ($extracted, $remainder) = extract_tagged($text); print $extracted; #Error #Undefined subroutine &main::extract_bracketed called at C:\InstallV3\ +Test.pl

Replies are listed 'Best First'.
Re^2: Regular Expression problem when Extracting Start\ VALUE \End
by philcrow (Priest) on Sep 30, 2005 at 18:12 UTC
    Text::Balances does not export functions into the main namespace by default. This means you have two options. First, you could ask for the function by name:
    use Text::Balanced qw( extract_tagged ); # The rest of your code from above here.
    This will bring extract_tagged into your module's namespace.

    Alternatively, you could fully qualify the name:

    use Text::Balanced; my $text = 'sometexthere'; ($extracted, $remainder) = Text::Balanced::extract_tagged($text);
    Phil
      Thanks Phil for useful advice. BTW I created 3 subroutines that cover most of cases in Text::Balanced What do you think ?
      ###################################################################### +####### #Input file Looks Like: #SingleLinesMultiLineFile #START_TAG\ value1 \END_TAG #SomeChar #START_TAG\ value2 \END_TAG #SomeChar #START_TAG\ value3 \END_TAG #SomeChar #end SingleLinesMultiLineFile #Usage: #$InputFile="$LocationOfTheScriptsDirectory\\MultiLine.txt"; #$StartTag='START_TAG\\'; #$EndTag='\END_TAG'; #@AValues=getInfoFromSingleLinesMultiLineFile($InputFile,$StartTag,$En +dTag); #Result:@AValues=qw(value1 value2 value3); sub getInfoFromSingleLinesMultiLineFile { my ($InputFile,$StartTag,$EndTag)=@_; my ($line,@wanted_substrings); #Openning file for reading open(IFH,"$InputFile") || die "Can't open file: $InputFile\n"; while($line=<IFH>) { if($line =~ /\Q$StartTag\E(.*?)\Q$EndTag\E/) { push(@wanted_substrings,$1); } } return @wanted_substrings; } ###################################################################### +####### #Input file Looks Like: #MultipleLinesMultiLineFile #SomeChar # START #value # END #AnotherChar #end MultipleLinesMultiLineFile # #Usage: #$InputFile="$LocationOfTheScriptsDirectory\\MultyLine.txt"; #$StartTag='START'; #$EndTag='END'; #@StartEnd=getInfoFromMultipleLineMultiLineFile($InputFile,$StartTag,$ +EndTag); sub getInfoFromMultipleLinesMultiLineFile { my ($InputFile,$StartTag,$EndTag)=@_; my ($line,@wanted_substrings); #Openning file for reading open(IFH,"$InputFile") || die "Can't open file: $InputFile\n"; while($line=<IFH>) { if (($line =~ m/\Q$StartTag\E/) .. ($line =~ m/\Q$EndTag\E/)) { push(@wanted_substrings,$line); } } return @wanted_substrings; } ###################################################################### +####### #Input file for longline: #SingleLineSingleLineFile<A>AAA</A><B>BBB</B><C>CCC</C><A>123</A><D>DD +D</D><A>HHH</A><C>CCC</C>end SingleLineSingleLineFile #Usage: #$InputFile="$LocationOfTheScriptsDirectory\\LongLine.txt"; #$StartTag='<A>'; #$EndTag='<\A>'; #@AValues=getInfoFromSingleLineSingleLineFile($InputFile,$StartTag,$En +dTag); #Result:@AValues=qw(AAA 123 HHH); sub getInfoFromSingleLineSingleLineFile { #Openning file for reading my ($InputFile,$StartTag,$EndTag)=@_; my ($line,@wanted_substrings); open(IFH,"$InputFile") || die "Can't open file: $InputFile\n"; $line = <IFH>; #1 #my @wanted_substrings = $line =~ /<A>(.*?)<\/A>/g; #2 #push(@wanted_substrings,$1) while $line =~ /<A>(.*?)<\/A>/g; #3 while ($line =~ /\Q$StartTag\E(.*?)\Q$EndTag\E/g) { push(@wanted_substrings,$1); } return "@wanted_substrings"; }
        I think that if it works for you and you understand it, that is good. Further, if writing it taught you something, that is better. But, I also think that tools like Text::Balanced make it possible for me to think at a higher level without getting bogged down in regex details on standard tasks.

        Phil

      I have Balanced.pm in the path but when I run the simple code

      #!/usr/bin/perl use strict; use warnings; use Text::Balanced qw(extract_bracketed); my $text = "{{define:STOP,law:101A}{here:stop}}"; my @line = Text::Balanced::extract_braketed($text,'{}');
      the error "Text::Balanced doesn't contain an __DATA__ token at .... line 7" is returned. when I change the line
      my @line = Text::Balanced::extract_braketed($text,'{}');
      to
      my @line = extract_braketed($text,'{}');
      I get Undefined subroutine &main::extract_braketed called at ... line 7. Any suggestions are welcome?