0

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.

2
  • 2
    Regular expression will be powerful enough if and only if your tags cannot nest arbitrarily. But even then, I think they will become so complicated that you'd be better off writing a few lines of code in the programming language of your choice to distinguish the cases and only use regular expressions as a lower-level building block.
    – 5gon12eder
    Nov 23, 2015 at 6:44
  • 1
    What most languages call "regexp" are not actually regular expressions. E.g. Perl 5.12's and Ruby 1.9's regexp can parse non-regular languages, probably even all context-free languages. (Ruby's even have choice and named recursion, I wouldn't be surprised if someday someone got bored enough to prove them Turing-complete.) Nov 23, 2015 at 7:35

1 Answer 1

2

If your syntax makes no distinction between an opening tag and a self-closing tag, then you end up with this scenario:

<a>
  <a>
    Some text
  </a>
  <b>
    Some more text
  </b>

The closing tag of a refers to the first a tag or the 2nd? Indented like this, my intent is more clear, though you could equally see the following:

<a><a>Some text</a><b>Some more text</b>

The difficulty arises in how you interpret successive tags. Is b a root tag or is it under the first a tag?

It is your language afterall, and you can choose to interpret it either way, however I personally would prefer to remove ambiguity from the language entirely so that anyone using it would know how it is interpretted. Adding a slash to indicate a self-closing tag seems like the most logical conclusion that requires minimum effort on the part of someone writing it and has a clear meaning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.