Reputation: 3
I'm looking for a pattern to extract a string left of a c++ comment and the comment itself. The problem is, the left side can contain a single slash as well.
Example:
"abc/def//comment"
As a result, I would like to have 2 groups which contains the left side of the comment and the comment itself:
Any suggestions?
Upvotes: 0
Views: 523
Reputation: 36229
echo "abc/def//comment" | sed -r 's|(.*)//(.*)|\1 //\2|'
abc/def //comment
What about multiple pairs of slashes? Comments inside Strings are ok?
Upvotes: 0
Reputation: 170148
Assuming you're processing the file line-by-line, this regex will do what you want:
((?:(?!//).)*)(//.*)
or simply:
(.*?)(//.*)
I.e., group 1 contains abc/def
and group 2 contains //comment
.
Be aware that when this fails with string literals and multi-line comments (to name just two pit-falls):
"a string with // in it"
/*
// not a comment!
*/
Upvotes: 2