Here is one input
<some tag: 1 2 3>
Here is another input
<some tag: 1 2 3>
some extra data
</some tag>
Both inputs appear in the same string. It looks like this:
<some tag: 1 2 3>
<some tag: 4 5 6>
some extra data
</some tag>
Note: this is not XML parsing. I just happen to like the way XML looks.
These are two separate pieces of information, and I would like to be able to capture them correctly.
The first match should give
- 1 2 3
The second match should give
- 4 5 6
- some extra data
The name of the tag itself is not important; it's just used as an indicator to show that this is the data I'm looking for.
Is it possible to write a regular expression that will capture this correctly?
The regex I'm using is
<some tag:(.+?)>(([\s\S]*?)<\/some tag>)?
So basically, the first line is required, but everything afterwards is optional.
However, the problem here is when someone mixes the two forms of input: the first match would take this
1 2 3
But then it would continue and capture everything under it as well, since there exists a "closing" tag at the end. So the actual match is this:
First
1 2 3
Second
<some tag: 4 5 6>
some extra data
My other solution would be to simply make it so that you would write
<some tag: 1 2 3 />
<some tag: 4 5 6>
some extra data
</some tag>
So that I will have one regex to parse the ones that don't have any extra data, and then another regex to handle the ones that do, but if I could avoid writing in that extra /
that would be preferable.
I could potentially use a CFG to parse it, but I'd like to be able to just throw a regex at it, but I'm not sure if regex is powerful enough to support it.