Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

RegEx colon patterns like :q

I'm doing RegEx.Matches on a simple CSV string like this:

"Smith","Jones"

This is in VB .NET and the string is properly escaped (see below).  I want to find the number of quoted strings in that line, which should be two, since RegEx doesn't do overlaps in the Matches method.  Here are two different possibilities:

Regex.Matches("""Smith"",""Jones""", """[^""]*""").Count

Regex.Matches("""Smith"",""Jones""", ":q").Count

The .NET Documentation tells me that these patterns should return the same thing; but in fact the first works (and returns 2), but the second returns 0.

Also:
*Using a RegEx object with the match pattern in the constructor makes no difference
*I get the same results in C#
*None of the other : patterns seem to work either
*I'm using VS .NET 2002 Academic
*The : patterns _do_ work in Find in the VS .NET IDE.

Can anyone shed some light on this situation, and especially on why the : patterns don't work in my code?

Kyralessa
Monday, April 19, 2004

Sorry,  but I'm not a huge .NET expert and I've never seen those character classes before (:q, :...).

It looks like they're invalid as:

Regex.Matches(":q",":q").Count; // 1

What MSDN docs are you reading?

Koz
Monday, April 19, 2004

The Regexes in the VS.NET IDE are completely different to the ones in the framework. Annoying but true. They're a hangover from previous versions of the IDE (Developer Studio).

In the ".NET Framework General Reference" look for the topic "Regular Expression Language Elements".

Use the Regulator to test your .NET Regexes:
http://royo.is-a-geek.com/iserializable/regulator/

Duncan Smart
Monday, April 19, 2004

"What MSDN docs are you reading?"

This:
ms-help://MS.VSCC/MS.MSDNVS/vsintro7/html/vxgrfRegularExpressionsS.htm

Kyralessa
Monday, April 19, 2004

You should be looking at:
ms-help://MS.NETFrameworkSDKv1.1/cpgenref/html/cpconregularexpressionslanguageelements.htm

or:
http://msdn.microsoft.com/library/en-us/cpgenref/html/cpconregularexpressionslanguageelements.asp

Duncan Smart
Monday, April 19, 2004

Yeah, now that I think back, I've never actually used RegEx in code before; all the regular expression stuff I did was on text files open in the IDE, where the :q thing works.

Well, at least I've got a valid expression to do what I need to do; mostly I was just curious why what I thought would work didn't work.  And now I've learned something new, saving future head-beatings-against-wall.  Thanks to all.

Kyralessa
Monday, April 19, 2004

So here's another question: Where do I find a reference list of what characters I can use in .NET regular expressions?  In the .NET Help and online I've found a number of articles that say "Here are a few examples of things you can do..." but that doesn't help me; I'm looking for a list like the VS IDE list I referred to.

Kyralessa
Wednesday, April 21, 2004

Here's the full list: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsintro7/html/vxgrfregularexpressionss.asp

Jon Galloway
Thursday, April 22, 2004

Um...that would be the list that got me mixed up in the first place, the list that includes VS .NET IDE-only syntax.

Kyralessa
Thursday, April 22, 2004

*  Recent Topics

*  Fog Creek Home