Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Answer: How do I extract all text between two keywords like start and end?

( #7674=categorized answer: print w/ replies, xml ) Need Help??

Q&A > regular expressions > How do I extract all text between two keywords like start and end? contributed by little_mistress

I'm kinda wondering about this one. Since you know the structure of the data (ie. the data starts after delimiter 'a' and ends with delimter 'b') and you allow the key word to be a regular word in the data I would have to assume your the $text in chromatics answer means that you have all the text in the same string. As I recall, sorry I've lost my mastering regular expressions book, its in Japan with Sawako, you need a regular expression that treats the newline charactor as an embeded charactor rather than the end of a line.

/start(.*)end/s; #rather than /start(.*)end/; #used like this $file = 'C:\fixthis.txt'; open(SESAME, $file); while(<SESAME>) { $text .= $_; } close(SESAME); print $text; $text=~/\n*$//;#get rid of trailing newlines $text=~m/^start(.*)end$/s; print $1; ########the file has this data ########## # I inserted alot of the words start and end to test it. #start #this is the start house startthat jackstart built #and i am end my fathers endchild #all end good boys do finend #and i eat end more chicken than any man that you have seen #end #############the out put is this############# #this is the start house startthat jackstart built #and i am end my fathers endchild #all end good boys do finend #and i eat end more chicken than any man that you have seen
That seems to work ok, if i understood the structure of your data correctly. If not im sure you could modify the regular expression to fit your needs.

Remember, simple is better.

little_mistress@mainhall.com

Comment on Answer: How do I extract all text between two keywords like start and end?
Download Code
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (12)
As of 2014-12-19 13:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (82 votes), past polls