I am trying to come up with universal code :) that will extract value between $StartTag value $EndTag from single line. I am having problem when special characters are involved.
my $line = <DATA>;
my @wanted_substrings=();
#No Problem
#my $StartTag='START';
#my $EndTag='END';
#Error Unmatched ) in regex; marked by <-- HERE in m/TRicky\(.*?)
#my $StartTag="TRicky\\";
#my $EndTag="\\endTricky";
#No Error but no value VALUE
#my $StartTag="Next\$";
#my $EndTag="\^Next";
#No Error but no value VALUE
my $StartTag="Last\+";
my $EndTag="\+some";
if ($line=~/$StartTag(.*?)$EndTag/g)
{
push(@wanted_substrings,$1) ;
}
print join "\n", @wanted_substrings;
__DATA__
CharSTARTanotherENDCharTRicky\VALUEE\endTrickyNext$VALUE^NextLast+VALU
+E+some
#Forgot to mention if I do not use $StartTag or $EndTag
# and insted use actual string that it will work.
#Instead
if ($line=~/$StartTag(.*?)$EndTag/g)
#This one works
if ($line=~/TRicky\\(.*?)\\endTricky/g)
#Problem is that I have to use varialble $ because
#I am using it as an arg in my sub
sub getInfoFromSingleLineMultiLineFile
{
#$stag,$etag uses as arguments
my ($InputFile,$stag,$etag)=@_;
my ($line,@wanted_substrings);
#Openning file for reading
open(IFH,"$InputFile") || die "Can't open file: $InputFile\n";
while($line=<IFH>)
{
if ($line =~ m/$stag(.*?)$etag/g)
{
push(@wanted_substrings,$1);
}
}
return @wanted_substrings;
}
Thanks in advance
Gasho
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.