Replacing SQL Comments using Regular Expressions

Results 1 to 2 of 2

Thread: Replacing SQL Comments using Regular Expressions

  1. #1
    Join Date
    Dec 1969

    Default Replacing SQL Comments using Regular Expressions

    I am currently working on a SQL validator for ASP and am wanting to remove the comments from the SQL before I run it through SQLDMO. As you know there are two styles for SQL comments; inline ("--") and block("/**/"). Anyway I have created a Regular Expression that removes the inline comments, and I thought that I had one for block style, but after some testing I reallized I was wrong. This is the regular expression I used: <BR>(/*)((.&#124
    ){0,})(*/)<BR>It does fine if there is only one comment block in the SQL, but if there are multiple blocks such as:<BR>/*This is a Comment*/ <BR>select blah from table_blah<BR>/*This is also a comment*/<BR><BR>it matches the whole string not just the commented sections. Does anyone know a Regular Expression that would just match the commented sections (I&#039;m replacing them with empty strings.) Any help would be appreciated.<BR><BR>Thanks,<BR><BR>Matt

  2. #2
    Join Date
    Dec 1969

    Default Don't be greedy!

    The reason is because the * character is known as a greedy matching symbol - it looks for as many characters as it can. We need to tell the regexp engine to use NON-greedy pattern matching, which is done using *? (instead of *)<BR><BR>Of course, there are some caveats - non-greedy matching only works in VBScript v5.5 and greater.<BR><BR>I HIGHLY recommend that you check out this article:<BR><BR>Picking Out Delimited Text with Regular Expressions <BR><BR><BR>hth

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts