Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^3: Regular Expression problem when Extracting Start\ VALUE \End

by gasho (Beadle)
on Sep 30, 2005 at 20:36 UTC ( [id://496514]=note: print w/replies, xml ) Need Help??


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

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"; }
  • Comment on Re^3: Regular Expression problem when Extracting Start\ VALUE \End
  • Download Code

Replies are listed 'Best First'.
Re^4: Regular Expression problem when Extracting Start\ VALUE \End
by philcrow (Priest) on Sep 30, 2005 at 21:00 UTC
    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://496514]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-19 14:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found