Regular Expression Question - Matching Tags?

G

Guest

Hi all,

I need to match a set of tags:


[tag]stuff[subtag]sflsajfasdl[/subtag]asfkajsldkfj[/tag]


1. Any ideas on the best way to parse such a string?

2. Is there a way for RegEx to do tag matching (i.e. [tag] must have a
[/tag]?)
 
G

Guest

Tom said:
Is there any reason to use that custom tag format? Why not use XML?

The application is a desktop app with web based components. I don't want to
use use XML for formatting because people could inject their own HTML into
the document. Using non-HTML/XML tags would it make it much easier to parse
and filter.

Anyone have suggestions on how to parse a custom tag format?
 
J

jamil

Try the following regular expression:

\[(\w+)\](.*)\[/\1\]

Repeat this regex match on the results from group two recursively
until it no longer finds a match.

So, with your example, group two would contain the following text with
the first match:

stuff[subtag]sflsajfasdl[/subtag]asfkajsldkfj

Repeat with the same regex, and group two would then return the
following:

sflsajfasdl

Turn the 'dot matches newline' option on.
 

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

Top