Text Processingflags: gm
Single-Line Comment (// or #)
Match single-line comments using either the `//` (C-family) or `#` (shell, Python, Ruby, YAML) marker.
Try it in RegexPro →Available in
Pattern
regexengine-agnostic
(?://|#).*$ (flags: gm)Raw source: (?://|#).*$
How it works
(?://|#) is a non-capturing group matching either marker. .*$ matches the rest of the line up to the newline. The g flag finds every comment; the m flag makes $ anchor at line boundaries instead of just end-of-string.
Examples
Input
var x = 1; // assignment\n# python style\ny = 2Matches
// assignment# python style
Input
Just a // sample lineMatches
// sample line
Input
no comments hereNo match
—Common use cases
- •Stripping line comments before evaluation
- •Linting rules for TODO/FIXME flagging
- •Documentation extraction (e.g. README from comments)
- •Cross-language source analysis