Jump to content
Sign in to follow this  
Toady

RegExp Help

Recommended Posts

Toady

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

Share this post


Link to post
Share on other sites
Xenobiologist

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

Share this post


Link to post
Share on other sites
Toady

A comment in VB starts with a ' character.

Well I think i figured it out... I have to check wether its at end of string..

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

www.itoady.com

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

Share this post


Link to post
Share on other sites
SadBunny

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.

Share this post


Link to post
Share on other sites
Toady

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

Share this post


Link to post
Share on other sites
Xenobiologist

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

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.