Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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

In reply to Regular Expression problem when Extracting Start\ VALUE \End by gasho

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-03-28 09:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found