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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:59:36+00:00 2026-05-21T03:59:36+00:00

I need a regex or something to remove this kind of comments. /*! *

  • 0

I need a regex or something to remove this kind of comments.

/*!
 * Foo Bar
 */

I tried with /(\/*!.**\/)/m but fails. Any Suggestion?

  • 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-21T03:59:36+00:00Added an answer on May 21, 2026 at 3:59 am

    To do it accurately and efficiently, there is a better regex:

    regexp = /\/\*![^*]*\*+(?:[^*\/][^*]*\*+)*\//
    result = subject.gsub(regexp, '')
    

    Jeffrey Friedl covers this specific problem at length (using C-comments as an example) in his classic work: Mastering Regular Expressions (3rd Edition). Here is a breakdown of the regex which illustrates the “Unrolling-the-Loop” efficiency technique.

    regexp_long = / # Match she-bang style C-comment
        \/\*!       # Opening delimiter.
        [^*]*\*+    # {normal*} Zero or more non-*, one or more *
        (?:         # Begin {(special normal*)*} construct.
          [^*\/]    # {special} a non-*, non-\/ following star.
          [^*]*\*+  # More {normal*}
        )*          # Finish "Unrolling-the-Loop"
        \/          # Closing delimiter.
        /x
    result = subject.gsub(regexp_long, '')
    

    Note that this regex does not need Ruby’s ‘m’ dot-matches-all modifier because it does not use the dot!

    Additional: So how much more efficient is this regex over the simpler /\/\*!.*?\*\//m expression? Well using the RegexBuddy debugger, I measured how many steps each regex took to match a comment. Here are the results for both matching and non-matching: (For the non-,matching case I simply removed the last / from the comment)

    /*!
     * This is the example comment
     * Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar
     * Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar
     * Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar
     */
    
    '
    REGEX                        STEPS TO: MATCH  NON-MATCH
    /\/\*!.*?\*\//m                        488      491
    /\/\*![^*]*\*+(?:[^*\/][^*]*\*+)*\//    23       29
    '
    

    As you can see, the lazy-dot solution (which must backtrack once for each and every character in the comment), is much less efficent. Note also that the efficiency difference is even more pronounced with longer and longer comments.

    CAVEAT Note that this regex will fail if the opening delimiter occurs inside a literal string, e.g. “This string has a /*! in it!”. To do this correctly with 100% accuracy, you will need fo fully parse the script.

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

Sidebar

Related Questions

I need a regex to match something like 4f0f30500be4443126002034 and 4f0f30500be4443126002034>4f0f31310be4443126005578 but not like
I need a regex that matches all strings ending in .cs, but if they
I need a regex for valid url image, example: http://domain.something.jpg protocol: http,https,ftp estension: jpg,png,gif,bmp
I need a regex pattern, that will search the file for lines where something
I hope this isn't a repetition... I need a regex to do what should
I need to find, process and remove (one by one) any substrings that match
Possible Duplicate: What is the ultimate postal code and zip regex? I need Regex
I need a Regex that will match a java method declaration. I have come
I need a regex for the following pattern: Total of 5 characters (alpha and
I need a Regex Statement (run in c#) that will take a string containing

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.