Hi,

I'm having a problem with a regex spanning multiple lines. It returns all
matches in a single one, instead of multiple matches. The code I'm using is
this:

Regex regex = new Regex(@"^<Location /[A-Za-z0-9/_-]+>[\s\S]+^</Location>",
RegexOptions.Multiline);

And the input text is this:

<Location /oceans/pacific>
DAV svn
SVNParentPath c:\repositories\pacific

AuthType Digest
AuthName "Pacific Ocean"
AuthDigestFile admin/svn-users
AuthDigestDomain /oceans
AuthzSVNAccessFile admin/svn-policy
Require valid-user
</Location>

<Location /oceans/atlantic>
DAV svn
SVNParentPath c:\repositories\atlantic

AuthType Digest
AuthName "Atlantic Ocean"
AuthDigestFile admin/svn-users
AuthDigestDomain /oceans
AuthzSVNAccessFile admin/svn-policy
Require valid-user
</Location>

It should return two matches. However, it returns only one match containing
all the text between the first occurrence of <Location through the last
occurrence of </Location>.

What am I missing here?

Thanks,

Celio Cidral Junior

Re: Regex problem when spanning multiple lines by Damien

Damien
Fri Oct 21 01:50:42 CDT 2005

Celio Cidral Junior wrote:
> Hi,
>
> I'm having a problem with a regex spanning multiple lines. It returns all
> matches in a single one, instead of multiple matches. The code I'm using is
> this:
>
> Regex regex = new Regex(@"^<Location /[A-Za-z0-9/_-]+>[\s\S]+^</Location>",
> RegexOptions.Multiline);
>
> And the input text is this:
>
> <Location /oceans/pacific>
> DAV svn
> SVNParentPath c:\repositories\pacific
>
> AuthType Digest
> AuthName "Pacific Ocean"
> AuthDigestFile admin/svn-users
> AuthDigestDomain /oceans
> AuthzSVNAccessFile admin/svn-policy
> Require valid-user
> </Location>
>
> <Location /oceans/atlantic>
> DAV svn
> SVNParentPath c:\repositories\atlantic
>
> AuthType Digest
> AuthName "Atlantic Ocean"
> AuthDigestFile admin/svn-users
> AuthDigestDomain /oceans
> AuthzSVNAccessFile admin/svn-policy
> Require valid-user
> </Location>
>
> It should return two matches. However, it returns only one match containing
> all the text between the first occurrence of <Location through the last
> occurrence of </Location>.
>
> What am I missing here?
>
> Thanks,
>
> Celio Cidral Junior

Hi,

Your "+" signs should be changed to "#" signs. "+" says "grab as much
input as possible that matches the expression. "#" says "grab as little
input as possible that matches the expression".

Damien


Re: Regex problem when spanning multiple lines by Oliver

Oliver
Fri Oct 21 03:15:00 CDT 2005

Celio Cidral Junior wrote:

>What am I missing here?

You're missing the greedy match. Try this instead:

<Location /[A-Za-z0-9/_-]+>[\s\S]+?</Location>

The difference (apart from removing two useless start-of-line markers) is
that there's a ? behind the final +. This makes the + match non-greedily,
meaning it finds the first possible full match instead of matching as much
as possible, which it does by default.


Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Re: Regex problem when spanning multiple lines by Oliver

Oliver
Fri Oct 21 03:53:55 CDT 2005

Damien wrote:

>Your "+" signs should be changed to "#" signs. "+" says "grab as much
>input as possible that matches the expression. "#" says "grab as little
>input as possible that matches the expression".

Never heard of that syntax and it doesn't seem to work for me either.
Where did you get that from?


Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Re: Regex problem when spanning multiple lines by Damien

Damien
Fri Oct 21 05:06:47 CDT 2005

Oliver Sturm wrote:
> Damien wrote:
>
> >Your "+" signs should be changed to "#" signs. "+" says "grab as much
> >input as possible that matches the expression. "#" says "grab as little
> >input as possible that matches the expression".
>
> Never heard of that syntax and it doesn't seem to work for me either.
> Where did you get that from?
>
>
> Oliver Sturm
> --
> Expert programming and consulting services available
> See http://www.sturmnet.org (try /blog as well)
Hi Oliver,

Cheers for helping to correct that. Pulled up the wrong regular
expressions help (the syntax I showed is for Regular expressions in the
"Find" dialog). The documentation is incomplete in VS2005, but I do not
believe that they've changed this. So we still have two different regex
syntaxes to use.

You'd think it would be an easy one to add into the Find Dialog (as an
additional option, so as not to confuse people who only use regexes
there) as a good example of having a large non-managed application
making use of pieces of the framework.

Ho hum.

Damien