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

  • SEARCH
  • Home
  • 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 6852961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:27:15+00:00 2026-05-27T01:27:15+00:00

I am working in VB.Net and trying to use Regex.Replace to format a string

  • 0

I am working in VB.Net and trying to use Regex.Replace to format a string I am using to query Sql. What Im going for is to cut out comments “–“. I’ve found that in most cases the below works for what I need.

string = Regex.Replace(command, "--.*\n", "")

and

string = Regex.Replace(command, "--.*$", "")

However I have ran into a problem. If I have a string inside of my query that contains the double dash string it doesn’t work, the replace will just cut out the whole line starting at the double dash. It makes since to me as to why but I can’t figure out the regular expression i need to match on.

logically I need to match on a string that starts with “–” and is not proceeded by “‘” and not followed by “‘” with any number of characters inbetween. But Im not sure how to express that in a regular expression. I have tried variations of:

string  = Regex.Replace(cmd, "[^('.*)]--.*\n[^(.*')]", "")

Which I know is obviously wrong. I have looked at a couple of online resources including http://www.codeproject.com/KB/dotnet/regextutorial.aspx
but due to my lack of understanding I can’t seem to figure this one out.

  • 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-27T01:27:16+00:00Added an answer on May 27, 2026 at 1:27 am

    I think you meant “match on a string that starts with -- and is not proceededpreceeded by ' and not followed by ' with any number of characters inbetween”

    If so, then this is what you are looking for:

    string  = Regex.Replace(cmd, "(?<!'.*?--)--(?!.*?').*(?=\r\n)", "")
    'EDIT: modified a little
    

    Of course, it means you can’t have apostrophes in your comments… and would be exceedingly easy to hack if someone wanted to (you aren’t thinking of using this to protect against injection attacks, are you? ARE YOU!??!  😀 )

    I can break down the expression if you’d like, but it’s essentially the same as my modified quote above!

    EDIT:

    I modified the expression a little, so it does not consume any carriage return, only the comment itself… the expression says:

    (?<!     # negative lookbehind assertion*
      '      #   match a literal single quote
      .*?    #   followed by anything (reluctantly*)
      --     #   two literal dashes
    )        # end assertion
    --       # match two literal dashes
    (?!      # negative lookahead assertion
      .*?    #   match anything (reluctant)
      '      #   followed by a literal single quote
    )        # end assertion
    .*       # match anything
    (?=      # positive lookahead assertion
      \r\n   #   match carriage-return, line-feed
    )        # end assertion
    
    • negative lookbehind assertion means at this point in the match, look backward here and assert that this cannot be matched
    • negative lookahead assertion means look forward from this point and assert this cannot be matched
    • positive lookahead asserts the following expression CAN be matched
    • reluctant means only consume a match for the previous atom (the . which means everything in this case) if you cannot match the expression that follows. Thus the .*? in .*?-- (when applied against the string abc--) will consume a, then check to see if the -- can be matched and fail; it will then consume ab, but stop again to see if the -- can be matched and fail; once it consumes abc and the -- can be matched (success), it will finally consume the entire abc--
      • non-reluctant or “greedy” which would be .* without the ? will match abc-- with the .*, then try to match the end of the string with -- and fail; it will then backtrack until it can match the --
    • one additional note is that the . “anything” does not by default include newlines (carriage-return/line-feed), which is needed for this to work properly (there is a switch that will allow . to match newlines and it will break this expression)

    A good resource – where I’ve learned 90% of what I know about regex – is Regular-Expressions.info

    Tread carefully and good luck!

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

Sidebar

Related Questions

Im trying to use the ComAutomationFactory class in VB .NET the example im working
I'm trying to use application settings with a C#.NET project I am working on.
I've been trying to get codeigniter working on .Net platform with the use of
I'm working on a project in C#.Net 4.0. I am trying to use HttpUtility.HtmlDecode.
I'm trying to use jquery meio mask with asp.net page. it is working ok
I am trying to use figure out how xpath is working in FireFox, but
I'm trying to use Jquery grid with asp.net, but its not working, it shows
I am working with CruiseControl.Net and am trying to take the dry approach as
I am trying to get MSTest working with CruiseControl.Net. I currently have it working
I am working on a VB.Net project and trying to get it to pull

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.