Jump to content

RegExp Help


Toady
 Share

Recommended Posts

I'm having difficulty in removing a comment from a line in VB.

Here is an example line...

Dim c As Char 'This is a comment

Im trying to use regexp to erase the comment but have trouble with code lines that look like this..

Dim s As String = 'blah' ' 'This is a comment

So this...

StringRegExpReplace($fileArray[$i],"'.*","")

Won't work for every case... any ideas?

Edited by Toady

www.itoady.com

A* (A-star) Searching Algorithm - A.I. Artificial Intelligence bot path finding

Link to comment
Share on other sites

Hi,

sorry do not know vb. What kind of comments do exist?

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

I guess the first thing you need is a clear definition of what the exact algorithm for recognizing comments is (so you can distinguish them from strings etc). Are all your comments placed behind 1 ' and all your strings placed between two? (I.E. You never use apostrophes in comments themselves?) If so, then I would suggest to look for lines containing an UNeven number of apostrophes, because then you know that you have a comment in it, then just search from the right side of the string using StringInStr's fourth parameter.

If you use apostrophes more loosely than that, it will be harder to tell comments apart from the actual code and you will need a more complex conceptual description of what exactly defines a comment, then when you know that exactly, you can 'translate' it to some RegEx query or a function or whatever you need.

Examples of what I mean: how would you go about finding the comments in the following lines, if you use them like this?

Dim s As String = 'blah' & 'blah' 'This is a comment

Dim s As String = 'blah' & 'blah' 'This is a comment with an extra apostrophe at the end'

Dim s As String = 'blah' & 'blah' 'This is a comment and it's a really good one

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Well I believe I have found the solution... Since you can only comment code at the end of every line. Then the regular expressions would be "'.*$"

This works for cases..

Dim c as string = "string" 'Comment

Dim c as string = "string" 'Comment'

Dim c as string = "string" 'Comment' something after comment

www.itoady.com

A* (A-star) Searching Algorithm - A.I. Artificial Intelligence bot path finding

Link to comment
Share on other sites

Hi,

and what do you do when the line is

Dim s As String = 'blah'

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...