It's regex time again - bigger and badder than ever before

C

C3

Hi everyone,

I have a text line that looks something like the following.

text1
text2
HEADER_1
interesting stuff
HEADER_2
text3
text4
text5
HEADER_3
more interesting stuff
....
HEADER_4
text6
text7
HEADER_3
even more interesting stuff
HEADER_4
text8
text9
HEADER_1
unbelievably interesting stuff
HEADER_2

HEADER_1 and HEADER_2 are corresponding opening and closing tags for one
type of content, with a similar arrangement for HEADER_3 and HEADER_4.

It is possible to write a regex to match either HEADER_1 or HEADER_3, but
can this be extended to match the content, and HEADER_2 and HEADER_4? Is it
possible to write a single regex to match either HEADER_1 or HEADER_3
followed by content followed by the corresponding HEADER_2 or HEADER_4 which
depends on whether HEADER_1 or HEADER_3 was matched?

I need to grab everything between HEADER_1 and HEADER_2, and everything
between HEADER_3 and HEADER_4, while keeping it in order. What would be the
simplest and/or cleanest way of doing this?


Thanks.

C3
 
T

Tad McClellan

C3 said:
I have a text line that looks something like the following.
[snip]

I need to grab everything between HEADER_1 and HEADER_2, and everything
between HEADER_3 and HEADER_4, while keeping it in order. What would be the
simplest and/or cleanest way of doing this?


The way that the Perl FAQ suggests.

perldoc -q lines

How can I pull out lines between two patterns that are themselves on difâ€
ferent lines?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,043
Members
48,769
Latest member
Clifft

Latest Threads

Top