Regex problem when spanning multiple lines

G

Guest

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
 
D

Damien

Celio said:
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
 
O

Oliver Sturm

Celio said:
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
 
O

Oliver Sturm

Damien said:
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
 
D

Damien

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


Oliver Sturm
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
 
Top