Re: parsing VB code with a regex

Tech-Archive recommends: Fix windows errors by optimizing your registry



You need to define your rules more exactly. Examples are not specific
enough. For example, you wrote:

I must create a routine that finds tokens in small, arbitrary VB code
snippets. For example, it might have to find all occurrences of
{Formula}

This leaves a lot of room for interpretation, something which computers are
extremely poor at, and humans not much better. The word "token" simply means
a series of characters without spaces between them. "{Formula}" as an
example, without any rules, implies nothing. I cannot, for example, assume
that by this example, your tokens will or must always have curly brackets
around them. It does not necessarily imply whether or not spaces may appear
between the curly brackets (if required) and the characters inside them.

Your example shows:

(1) should find {Area} (both occurrences) and {Height} in this string
If {Area} > 100 Then Return {Area} Else Return {Height}

Again, are the characters always supposed to be surrounded by curly
brackets? Should they have curly brackets at all, or are you just using them
to "highlight" what you are talking about?

(2) should find {Area}, but not {AreaString} in this string
If {Area} = "{AreaString}" 100 Then Return "Found it!"

What should it match in the following example?
If {Area} = "{Area String}" 100 Then Return "Found it!"

How about this one?

If {Area} = {"Area" "String") Then Return "Found it!"

(3) should find {Height}, but not {Area} in this multi-line string
'the {Area} token is not used here
If {Height} > 1000 Then
Return "Tall"
Else
Return "Short"
End If

Does this mean that it should ignore commented lines?

The first step to writing a regular expression is to define the rules that
comprise the pattern to match. If you can define these rules without any
examples (that is, if the rules are exactly defined, no examples will be
needed), I can write you a regular expression.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Development Numbskull

Nyuck nyuck nyuck


"Mark" <emby@xxxxxxxxxxxxxxxxxx> wrote in message
news:%23Pyl57QiGHA.4080@xxxxxxxxxxxxxxxxxxxxxxx
I must create a routine that finds tokens in small, arbitrary VB code
snippets. For example, it might have to find all occurrences of
{Formula}

I was thinking that using regular expressions might be a neat way to solve
this, but I am new to them. Can anyone give me a hint here?

The catch is, it must only find tokens that are not quoted and not
commented; examples follow

(1) should find {Area} (both occurrences) and {Height} in this string
If {Area} > 100 Then Return {Area} Else Return {Height}

(2) should find {Area}, but not {AreaString} in this string
If {Area} = "{AreaString}" 100 Then Return "Found it!"

(3) should find {Height}, but not {Area} in this multi-line string
'the {Area} token is not used here
If {Height} > 1000 Then
Return "Tall"
Else
Return "Short"
End If

I've searched many web sites and libraries, but they all seem to be
interested in finding quoted strings, not avoiding them. I'd appreciate
any help.

Emby




.



Relevant Pages

  • Re: Parser Help...
    ... you're defining a type built via one or more of the following tokens, ... I am building a simple parser, which can extract ... The struct is declared via "typedef struct", ... contents is listed inside the curly brackets (the type, ...
    (borland.public.delphi.non-technical)
  • parsing VB code with a regex
    ... I must create a routine that finds tokens in small, ... it might have to find all occurrences of ... but not in this multi-line string ... interested in finding quoted strings, ...
    (microsoft.public.dotnet.general)