Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Michael Ash's Regex Blog

Regex Musings

Comments?

I learned a few things, the hard way, about regex  comments in VB.

I was reading the Five Habits for Successful Regular Expressions and the first item is uses of whitespace and comments.  Now I've down this on several of my regexps on the regexlib site, but I haven't really done this in code.  In fact I've even removed comments so I could use a regex I had posted in some code.. A simple fact is adding comments in good in any coding environment regular expressions are no exception.   So I thought I'd look a doing a better job of commenting my expressions in my code.  That's when a problem dawned on me.  How do I added comments in VB?  In VB each statement has to be on its own line and the CRLF is the line terminator.   Unfortunately for me that I didn't find this entry from Darren until after I figured out how to do.  But through that trail and error process I did learn something else along the way.  You can't stick in-line comments (?#...) anywhere you please. I was trying to use a date regex that was heavily commented with both End-of-Line regex comments and in-line comments.   I tried changing all the EOL comments to in-line comments when I got a regex syntax error.  The regex has a conditional grouping in it (?(?=...)True|False). In the original version it was formatted

(? # comment 1

     (?=... # More comments

     )

          True # Yada yada yada

          |

          False # Comments for everyone!

)

changing to

(? (?# comment 1)

(?=... (?# More comments)

)

True (?# Yada yada yada)

|

False (?# Comments for everyone!)

)

failed because you can't put an in-line comment where (?# comment 1) is located.

Sponsor
Published Sunday, July 11, 2004 12:48 AM by mash
Filed under:

Comments

No Comments
Anonymous comments are disabled