BytePane

SQL Comment Regex Pattern

Matches both single-line (--) and multi-line (/* */) SQL comments for stripping or extracting documentation.

{}
Data Formats
Intermediate
Difficulty
SQL
Language
gm
Flags
// Regular Expression
/--.*$|\/\*[\s\S]*?\*\//gm

Live Regex Tester

Pattern Breakdown

--.*$|\/\*[\s\S]*?\*\/
Character class [ ]
Group ( )
Quantifier { }
Anchor ^ $
Repetition * + ?
Escape \
Alternation |
Any char .

Code Examples

JavaScript

const regex = /--.*$|\/\*[\s\S]*?\*\//gm;
const test = "-- This is a comment";
console.log(regex.test(test)); // true

// Extract matches
const matches = test.match(regex);
console.log(matches);

Python

import re

pattern = r'--.*$|\/\*[\s\S]*?\*\/'
test = "-- This is a comment"
match = re.findall(pattern, test)
print(match)  # Found!

Go

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile(`--.*$|\/\*[\s\S]*?\*\/`)
    fmt.Println(re.MatchString("-- This is a comment")) // true
}

Common Use Cases

SQL cleanupquery extractioncode documentation

Match Examples

InputResult
-- This is a commentMatch
SELECT * FROM tNo Match

About the SQL Comment Regex

Matches both single-line (--) and multi-line (/* */) SQL comments for stripping or extracting documentation.

Regular expressions (regex) are powerful pattern matching tools used across virtually all programming languages. The sql comment pattern is classified as intermediate difficulty in the data formats category. This pattern is specifically designed for SQL.

When using this regex, always consider edge cases and test thoroughly with real-world data. Use the interactive tester above to validate the pattern against your specific inputs before deploying to production.

Need More Regex Patterns?

Browse our complete library of 100+ regex patterns with interactive testers.

Frequently Asked Questions

What is the SQL Comment regex pattern?

Matches both single-line (--) and multi-line (/* */) SQL comments for stripping or extracting documentation.

How do I use the SQL Comment regex?

Use the pattern /--.*$|\/\*[\s\S]*?\*\//gm in your code. In JavaScript: new RegExp('--.*$|\/\*[\s\S]*?\*\/', 'gm'). Test it above with your own input.

What does this SQL Comment regex match?

This pattern matches: "-- This is a comment". It does NOT match: "SELECT * FROM t". SQL cleanup, query extraction, code documentation.

Is the SQL Comment regex beginner-friendly?

This pattern is rated Intermediate. It uses some advanced features like character classes and quantifiers.

What languages support the SQL Comment regex?

This pattern works in SQL. Syntax may vary slightly between regex engines.

Can I modify the SQL Comment regex for my use case?

Yes! Use the interactive tester above to modify the pattern and test with your own data. Common modifications include making it case-insensitive (add 'i' flag), matching globally (add 'g' flag), or adjusting character classes.

Related Tools