Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 244029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:58:15+00:00 2026-05-11T20:58:15+00:00

i want to parse VB6 code via Regex. However being new to Regex I

  • 0

i want to parse VB6 code via Regex. However being new to Regex I have encountered a few problems concerning the regexes to use. Currently, I have problems recognizing these constructs:

' Subs
' Sub Test
Private Sub Test(ByVal x as Integer)
    'Private Sub Test(ByVal y as Integer)
    Dim dummy as String
    dummy = "Private Sub Test(ByVal y as Integer)"
End Sub 

I have basically these 2 problems: How do I write a Regex that matches the Sub definition, and includes the all commment (and empty) lines above its definition? And how can I prevent that the Sub definitions which are either disabled by comment or included in strings aren’t matched?
Plus I need to support definitions which are spanned over multiple lines, like this one:

' Subs
' Sub Test
Private Function Test2( _
   ByVal x as Integer _
) As Long
    'Private Sub Test(ByVal y as Integer)
    Dim dummy as String
    dummy = "Private Sub Test(ByVal y as Integer)"
End Function

Any hint would be greatly appreaciated. The solutions I’ve come up with don’t work with multiple lines or capture more than just one Sub definition. It then just matches to the end of the last End Sub occurrence due to greedy matching.

My try in C# currently looks like this:

(('(?<comment>[\S \t]+[\n\r]+))*((?<accessmodifier>(Private|Public))\s+_?)(?<functiontype>(Sub|Function))\s+_?(?<name>[\S]+)\((?<parameters>[\S \t]*)\)([ \t]+As[ \t]+(?<returntype>\w+))?)|(?<endfunction>End (Sub|Function))

I’m using Multiline, Singleline, IgnoreCase, ExplicitCapture.

Thanks for your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:58:15+00:00Added an answer on May 11, 2026 at 8:58 pm

    Why are you parsing this code? If you’re trying to create your own compiler, you’ll need a lot more than regexes. If you’re writing an editor with syntax highlighting and type-ahead completion, regexes can do a pretty good job on the first, but not the second.

    That said, the biggest problem I see with your regex is that you’re not handling line continuations properly. This: \s+_? matches one or more whitespace characters, optionally followed by an underscore. But if there is an underscore it should be followed by a newline, which you aren’t matching. That’s easy enough to remedy – \s+(_\s+)? – but I’m not sure you need to be that specific. I suspect this: [\s_]+ will do just as well.

    As for avoiding apparent sub/function declarations inside comments and strings, the simplest way would be to match them only at the left margin, with maybe some tabs or spaces for indentation. It’s cheating, I know, but it may be good enough for whatever you’re doing. I relied heavily on that trick when I was writing a Java file navigation scheme for EditPad Pro. You can’t do this kind of thing with regexes without employing lots of gimmicks and simplifying assumptions. Try this regex:

    ^(?>('(?<comment>.*[\n\r]+))*)
    [ \t]*(?<accessmodifier>(Private|Public))
    [\s_]+(?<functiontype>(Sub|Function))
    [\s_]+(?<name>\S+)
    [\s_]*\((?<parameters>[^()]*)\)
    ([\s_]+As[\s_]+(?<returntype>\w+))?
    |
    ^[ \t]*(?<endfunction>End (Sub|Function))

    Of course you’ll need to reassemble it first. It should be compiled with the Multiline, IgnoreCase and ExplicitCapture options, but not Singleline.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 136k
  • Answers 136k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In the military it's called "Defense in Depth". The theory… May 12, 2026 at 7:14 am
  • Editorial Team
    Editorial Team added an answer Sounds like you just need: items = CollectionItem.objects.filter( collection__is_public=True, collection__type=7… May 12, 2026 at 7:14 am
  • Editorial Team
    Editorial Team added an answer A great way to do this is to use negative… May 12, 2026 at 7:14 am

Related Questions

A lot of programs log things into a text file in a format something
I've come across a query in an application that I've inherited that looks like
I use a VB6/COM+ application which outputs date/time values based on the short date
I have a decryption routine in VB6. I now want the same decryption in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.