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 7489257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:13:18+00:00 2026-05-29T15:13:18+00:00

So I need a regular expression for finding single line and multi line comments,

  • 0

So I need a regular expression for finding single line and multi line comments, but not in a string. (eg. "my /* string")

for testing (# single line, /* & */ multi line):

# complete line should be found
lorem ipsum # from this to line end
/*
  all three lines should be found
*/ but not here anymore
var x = "this # should not be found"
var y = "this /* shouldn't */ match either"
var z = "but" & /* this must match */ "_"

SO does the syntax display really well; I basically want all the gray text.
I don’t care if its a single regex or two separates. 😉

EDIT: one more thing. the opposite would also satisfy me, searching for a string which is not in a comment
this is my current string matching: "[\s\S]*?(?<!\\)" (indeed: will not work with "\\")

EDIT2:
OK finally I wrote my own comment parser -.-
And if someone else is interested in the source code, grab it from here: https://github.com/relikd/CommentParser

  • 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-29T15:13:18+00:00Added an answer on May 29, 2026 at 3:13 pm

    Here’s one possibility (it does have an achilles heel that i’ll get to):

    (#[^"\n\r]*(?:"[^"\n\r]*"[^"\n\r]*)*[\r\n]|/\*([^*]|\*(?!/))*?\*/)(?=[^"]*(?:"[^"]*"[^"]*)*$)
    

    In action here

    With the GLOBAL and DOTALL flags, but not the MULTILINE flag.

    Explanation of the regex:

    (
      #[^"\n\r]*                         Hash mark followed by non-" and non-end-of-line
        (?:"[^"\n\r]*"[^"\n\r]*)*        If any quotes in the comment, they must be balanced
        [\r\n]                           Followed by end-of-line ($ except we 
                                          don't have multiline flag)
    
      |                                  OR
      /\*([^*]|\*(?!/))*?\*/             /* xxx */ sort of comment
      )                                  BOTH FOLLOWED BY
    (?=[^"]*(?:"[^"]*"[^"]*)*$)           only a *balanced* number of quotes for the 
                                          *rest of the code :O!*
    

    However, this relies on balanced quotes being used throughout the text (it also doesn’t take into account escaped quotes, but it’s easy enough to modify the regex to take that into account).

    If a user has a comment with a ” in it that isn’t balanced…boom. You’re screwed!

    Regex is generally not recommended by things like HTML/code parsing, but if you can rely on the fact that quotes have to balance when you define a string, etc, you can sometimes get away with it.

    Since you are also parsing comments, which have no set structure (ie you are not guaranteed that quotes within comments will be balanced), you won’t be able to find a regex solution that works here.

    Anything you think up can be outwitted by an unbalanced quote in a comment somewhere (say the comment was # remove all the " marks), or by multiline strings (where on a given line there may be unbalanced quotes).

    Bottom line – you can probably make a regex that will work in most cases, but not for all. To get something watertight you’ll have to write some code.

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

Sidebar

Related Questions

Need single regular expression for finding double spaces tabs extra enter (line-break/carriage return) line
Need a regular expression to validate username which: should allow trailing spaces but not
I need a regular expression able to match everything but a string starting with
I need a regular expression which matches: http://example.com/foo http://example.com/foo/ http://example.com/foo/bar but not: http://example.com/foobar Using
i need a Regular Expression to convert a a string to a link.i wrote
I need a regular expression to basically get the first part of a string,
Could you tell how to replace string by preg-replace (need regular expression): /user/{parent_id}/{action}/step/1 At
Need a regular expression to validate number with comma separator. 1,5,10,55 is valid but
I need a regular expression to help me make a match in my string.
I need a Regular expression that allows me to split the following string in

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.