re = /(?|
\G(?!\A) # contiguous to the precedent match (inside a comment)
(?|
-{2,}+([^->][^-]*) # duplicate hyphens, not part of the closing sequence
|
(-[^-]+) # preserve isolated hyphens
|
-+ (?=-->) # hyphens before closing sequence, break the contiguity
|
-->[^<]* (*SKIP)(*FAIL) # closing sequence, go to next <, break contiguity
)
|
[^<]*<+ # reach the next < (outside comment)
(?> [^<]+ <+ )*? # all characters until <!-- (include)
(?: !-- \K | [^<]*\z\K (*ACCEPT) ) # new comment or end of the string
(?|
-*+ ([^->][^-]*) # possible hyphens not followed by >
|
-+ (?=-->) # hyphens before closing sequence, break the contiguity
|
-?+ ([^-]+) # one hyphen followed by >
|
-->[^<]* (*SKIP)(*FAIL) () # closing sequence, go to next <, break contiguity
) # () avoids a misterious bug in regex101, you can remove it
)
/x
str = '<!----------------- toto -->
<!-- toto ---------------------------->
<!---->
<!----->
<!--->>>>--->
asdsd sadasdsad sadasd asdasd asdasd --> asd asdasd
<!--- ->>>>>>
---------------------------------------
***************************************
AUGMENTATION BLUEPRINTS
***************************************
---------------------------------------
--->----
<!-- toto-toto -->
<!--OTHER IDEAS --
<anytaghere>
<!----PROBABLY NOT:---
<whatever>
--><!--'
subst = '\\1'
result = str.gsub(re, subst)
# Print the result of the substitution
puts result
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html