Subject: Regex Help
Posted By: boyer99g Post Date: 10/8/2004 4:32:23 PM
Hi,

I need some help with a regular expression.  I'm trying to parse, out of text, tags in the following forms:

<%Foo%>
<%Bar("aasd", "213")%>

I want to get the name of the function/tag in a named group, and pull out the parameters (the text inside the "" for each).

Here is what I have so far for the pattern I pass to the Regex object constructor:

@"<%(?<NAMES>\w*)(\(""(?<ARGS>.*)""\))?%>"

This gets the names correctly ('Foo' and 'Bar'), but the ARGS group on ly contains the following:

    aasd", "213

I understand why I get this, but am at a loss as to how to get what I want.  Any help would be appreciated; I'm starting to get a little frustrated.  Thanks.

Reply By: bmains Reply Date: 10/8/2004 5:33:54 PM
<%(?<

Why is the ? there?  The ( is to group the first set of the regexp.  Second, in the params part, I see no check for a , or white space.

Brian
Reply By: boyer99g Reply Date: 10/8/2004 5:46:14 PM
Thanks for the respose.

The '?' is needed to signify a named group; it's necessary, and that part of the expression works fine.

I've since come up with this expression, which works better, but still isn't perfect:

@"<%(?<NAMES>\w*)(\((""(?<ARGS>[^""]*))+\))?%>"

With this, I get the separate parameters, but also the comma delimiters.    So if the input was <%Foo("bar", "bar")%>, the captures in the ARGS group would be (without the single quotes):

'bar'
', '
'bar'
'' <- empty string

Again, I realize why this is happening, but no matter what change to the expression I try, I only succeed in breaking it completely.


Go to topic 20439

Return to index page 749
Return to index page 748
Return to index page 747
Return to index page 746
Return to index page 745
Return to index page 744
Return to index page 743
Return to index page 742
Return to index page 741
Return to index page 740