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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:21:12+00:00 2026-06-13T13:21:12+00:00

I need to match all three types of comments that PHP might have: #

  • 0

I need to match all three types of comments that PHP might have:

  • # Single line comment

  • // Single line comment

  • /* Multi-line comments */

  •  

     /**
      * And all of its possible variations
      */
    

Something I should mention: I am doing this in order to be able to recognize if a PHP closing tag (?>) is inside a comment or not. If it is then ignore it, and if not then make it count as one. This is going to be used inside an XML document in order to improve Sublime Text‘s recognition of the closing tag (because it’s driving me nuts!). I tried to achieve this a couple of hours, but I wasn’t able. How can I translate for it to work with XML?

So if you could also include the if-then-else login I would really appreciate it. BTW, I really need it to be in pure regular expression expression, no language features or anything. 🙂

Like Eicon reminded me, I need all of them to be able to match at the start of the line, or at the end of a piece of code, so I also need the following with all of them:

<?php
    echo 'something'; # this is a comment
?>
  • 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-06-13T13:21:13+00:00Added an answer on June 13, 2026 at 1:21 pm

    Parsing a programming language seems too much for regexes to do. You should probably look for a PHP parser.

    But these would be the regexes you are looking for. I assume for all of them that you use the DOTALL or SINGLELINE option (although the first two would work without it as well):

    ~#[^\r\n]*~
    ~//[^\r\n]*~
    ~/\*.*?\*/~s
    

    Note that any of these will cause problems, if the comment-delimiting characters appear in a string or somewhere else, where they do not actually open a comment.

    You can also combine all of these into one regex:

    ~(?:#|//)[^\r\n]*|/\*.*?\*/~s
    

    If you use some tool or language that does not require delimiters (like Java or C#), remove those ~. In this case you will also have to apply the DOTALL option differently. But without knowing where you are going to use this, I cannot tell you how.

    If you cannot/do not want to set the DOTALL option, this would be equivalent (I also left out the delimiters to give an example):

    (?:#|//)[^\r\n]*|/\*[\s\S]*?\*/
    

    See here for a working demo.

    Now if you also want to capture the contents of the comments in a group, then you could do this

    (?|(?:#|//)([^\r\n]*)|/\*([\s\S]*?)\*/)
    

    Regardless of the type of comment, the comments content (without the syntax delimiters) will be found in capture 1.

    Another working demo.

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

Sidebar

Related Questions

I need CSS selector that match all input tags where type is not checkbox
I have a set of apps that all need to synchronize with a webservice:
Possible Duplicate: Regular expression: match all words except I need your help for using
I need to match with preg_match the urls of a certain domain that end
I have about 20 CSV's that all look like this: [email],[fname],[lname],[prefix],[suffix],[fax],[phone],[business],[address1],[address2],[city],[state],[zip],[setdate],[email_type],[start_code] What I've been
I have a table that lists people and all their contact info. I want
I have a very large XML file that I need to parse so I
I need to match and replace some comments. for example: $test = the url
I have a subclass of UIViewController that is responsible for a single UIWebView. Since
I have the need to search for an item that exists in one of

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.