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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:37:59+00:00 2026-05-27T10:37:59+00:00

Find Oracle single line comments except the ones that appear inside a string. For

  • 0

Find Oracle single line comments except the ones that appear inside a string.

For example:

-- This is a valid single line comment

But

'This is a string -- and it is not a comment';

I am using this regex to find single line comments

--.*$

a few cases can be handled but there are several complex ones. You can use this script for reference

-- this is a single line comment

CREATE OR REPLACE PROCEDURE "MAIL_WITH_ATTACHMENT" ( ) 
IS    
tmp varchar(2) ; -- this is a comment 
tmp1 varchar(2) := 'some texxt'; -- this is another comment
tmp2 varchar(3) := 'some more --text'; -- this is one more comment
tmp3 varchar(4) := 'this regex isn't --working properly'; -- Don't you think this is another comment
BEGIN

          '--This is a Mime message, which your current mail reader may not' || crlf ||
          ' some more -- characters in a string';

    mesg:= crlf ||
          '--This is a Mime message, which your current mail reader may not' || crlf ||
      ' some more -- characters in a string';
END; 

Result must be this

[1] : -- this is a single line comment
[2] : -- this is a comment 
[3] : -- this is another comment
[4] : -- this is one more comment
[5] : -- Don't you think this is another comment

Thanks

  • 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-27T10:37:59+00:00Added an answer on May 27, 2026 at 10:37 am

    Personally, I’d use an SQL parser to strip these comments. The problem with regex is that it’s not really aware of its surroundings: regex has a hard time figuring out if a single quote is inside a comment, or if -- is inside a string literal.

    You can circumvent this by using a regex that matches from the start of a line and match string literals as well. Making it behave more like a lexical analyzer (the first stage of parsing).

    Such a regex could look like this:

    (?m)^((?:(?!--|').|'(?:''|[^'])*')*)--.*$
    

    A quick break down of the regex:

    (?m)                 # enable multi-line mode
    ^                    # match the start of the line
    (                    # start match group 1
      (?:                #   start non-capturing group 1
        (?!--|').        #     if there's no '--' or single quote ahead, match any char (except a line break)
        |                #     OR
        '(?:''|[^'])*'   #     match a string literal
      )*                 #   end non-capturing group 1 and repeat it zero or more times
    )                    # end match group 1
    --.*$                # match a comment all the way to the end of the line
    

    In plain English that would read like: from each start of a line, try to match zero or more:

    • string literals ('(?:''|[^'])*');
    • or any character as long as it’s not a single quote, a line break char or a - that is a part of a comment ((?!--|').).

    and store this match in group 1. Then match a comment (--.*$).

    So now all you need to do is replace this pattern with whatever is matched in group 1. A demo:

    String sql = "-- this is a single line comment\n" +
                 "\n" +
                 "CREATE OR REPLACE PROCEDURE \"MAIL_WITH_ATTACHMENT\" ( ) \n" +
                 "IS    \n" +
                 "tmp varchar(2) ; -- this is a comment \n" +
                 "tmp1 varchar(2) := 'some texxt'; -- this is another comment\n" +
                 "tmp2 varchar(3) := 'some more --text'; -- this is one more comment\n" +
                 "tmp3 varchar(4) := 'this regex isn''t --working properly'; -- Don't you think this is another comment\n" +
                 "BEGIN\n" +
                 "\n" +
                 "          '--This is a Mime message, which your current mail reader may not' || crlf ||\n" +
                 "          ' some more -- characters in a string';\n" +
                 "\n" +
                 "    mesg:= crlf ||\n" +
                 "          '--This is a Mime message, which your current mail reader may not' || crlf ||\n" +
                 "      ' some more -- characters in a string';\n" +
                 "END; ";
    String stripped = sql.replaceAll("(?m)^((?:(?!--|').|'(?:''|[^'])*')*)--.*$", "$1[REMOVED COMMENT]");
    System.out.println(stripped);
    

    which will print:

    [REMOVED COMMENT]
    
    CREATE OR REPLACE PROCEDURE "MAIL_WITH_ATTACHMENT" ( ) 
    IS    
    tmp varchar(2) ; [REMOVED COMMENT]
    tmp1 varchar(2) := 'some texxt'; [REMOVED COMMENT]
    tmp2 varchar(3) := 'some more --text'; [REMOVED COMMENT]
    tmp3 varchar(4) := 'this regex isn''t --working properly'; [REMOVED COMMENT]
    BEGIN
    
              '--This is a Mime message, which your current mail reader may not' || crlf ||
              ' some more -- characters in a string';
    
        mesg:= crlf ||
              '--This is a Mime message, which your current mail reader may not' || crlf ||
          ' some more -- characters in a string';
    END; 
    

    EDIT

    And if you only want to extract the comments, wrap the capture group around --.*$ and use a Pattern & Matcher to find() the matches:

    Matcher m = Pattern.compile("(?m)^(?:(?!--|').|'(?:''|[^'])*')*(--.*)$").matcher(sql);
    while(m.find()) {
      System.out.println(m.group(1));
    }
    

    which will print:

    -- this is a single line comment
    -- this is a comment 
    -- this is another comment
    -- this is one more comment
    -- Don't you think this is another comment
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to find a tool for Oracle that is similar to the SQL
How can I find out the URL and port for an Oracle database? Example:
To find a specific date after some given date in SQL oracle. Example: Date
This is an Oracle question. I need to find the top 5 biggest values
How can write a query for Oracle database such that I can find a
I have an Oracle view that uses a table that I cannot find anywhere.
Using Oracle, how can I find index names and creation dates from systables/information_schema? How
I am using the REGEXP_LIKE function in Oracle 10g to find values in a
I'm trying to find an integer number of days between two dates in Oracle
I traced an oracle process, and find it first open a file /etc/netconfig as

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.