Skip to main content
minor edits/added tags/made title less generic
Source Link
Syed
  • 59.5k
  • 5
  • 40
  • 95

Extracting sequences delimited by two String markers from a list?

Suppose I have a list of arbitrary length like:

{1, 2, 3, "Open", 3, 2, "Close", 9, 3, 4, "Open", 1, 0, "Close", 3, 5}

and I am trying to extract the sequences insidedelimited by the open"Open"/close"Close" tags, i.e. the answer I want is:

{{3, 2}, {1, 0}}

What's the right way to do this? The data I am actually working with is a large XML document and I am trying to extract sections of it by identifying certain tags as boundaries. I've fiddled with different pattern matching functions but can't figure out how to operate on a list in this way since I am trying to match patterns on an intermediate level between individual elements and the entire list.

EDIT

To clarify, I don't know in advance how many such sequences the data will contain or how many elements will be insidepresent between any particular set of tags.

Extracting sequences from a list?

Suppose I have a list of arbitrary length like

{1, 2, 3, "Open", 3, 2, "Close", 9, 3, 4, "Open", 1, 0, "Close", 3, 5}

and I am trying to extract the sequences inside the open/close tags, i.e. the answer I want is

{{3, 2}, {1, 0}}

What's the right way to do this? The data I am actually working is a large XML document and I am trying to extract sections of it by identifying certain tags as boundaries. I've fiddled with different pattern matching functions but can't figure out how to operate on a list in this way since I am trying to match patterns on an intermediate level between individual elements and the entire list.

EDIT

To clarify, I don't know in advance how many such sequences the data will contain or how many elements will be inside any particular set of tags.

Extracting sequences delimited by two String markers from a list

Suppose I have a list of arbitrary length:

{1, 2, 3, "Open", 3, 2, "Close", 9, 3, 4, "Open", 1, 0, "Close", 3, 5}

and I am trying to extract the sequences delimited by the "Open"/"Close" tags, i.e. the answer I want is:

{{3, 2}, {1, 0}}

What's the right way to do this? The data I am actually working with is a large XML document and I am trying to extract sections of it by identifying certain tags as boundaries. I've fiddled with different pattern matching functions but can't figure out how to operate on a list in this way since I am trying to match patterns on an intermediate level between individual elements and the entire list.

EDIT

To clarify, I don't know in advance how many such sequences the data will contain or how many elements will be present between any particular set of tags.

Source Link
mfvonh
  • 8.5k
  • 27
  • 43

Extracting sequences from a list?

Suppose I have a list of arbitrary length like

{1, 2, 3, "Open", 3, 2, "Close", 9, 3, 4, "Open", 1, 0, "Close", 3, 5}

and I am trying to extract the sequences inside the open/close tags, i.e. the answer I want is

{{3, 2}, {1, 0}}

What's the right way to do this? The data I am actually working is a large XML document and I am trying to extract sections of it by identifying certain tags as boundaries. I've fiddled with different pattern matching functions but can't figure out how to operate on a list in this way since I am trying to match patterns on an intermediate level between individual elements and the entire list.

EDIT

To clarify, I don't know in advance how many such sequences the data will contain or how many elements will be inside any particular set of tags.