Wikipedia:Reference desk/Archives/Computing/2017 September 26

= September 26 =

Regex: How to delete all comments and all that comes after them in a row?
I tried this:

To delete all commets starting with #space (and that might be preceded by a space).

It does delete them, but not everything coming after them in the same row. Ben-Yeudith (talk) 06:34, 26 September 2017 (UTC)
 * Try "^ ?# .*" based on your description, if a comment is always on its own line. The last two characters (.*) means 'everything'. -- zzuuzz (talk) 07:11, 26 September 2017 (UTC)
 * Hmm, sadly this seems to delete everything in the file, not only comments. Ben-Yeudith (talk) 07:19, 26 September 2017 (UTC)
 * Depending on how you open the file, ".*" will usually match newline characters as well. ^ ?# [^\n]* could work (I have not tested it) ([^\n] means "any non-newline character"). Tigraan Click here to contact me 11:05, 26 September 2017 (UTC)
 * I'm far too used to multiline. There may be an option for 'multiline mode' in your editor or particular language, possibly /m or (?m), which would make the expression "(?m)^ ?# .*" -- zzuuzz (talk) 11:43, 26 September 2017 (UTC)


 * If you want to express " lines that begins with # or with space and #" that won't be

?^


 * This

^


 * matches the beginning, there's nothing before the beginning, optional or not. Make the space optional.B8-tome (talk) 12:20, 26 September 2017 (UTC)


 * The following worked. Thanks.

Ben-Yeudith (talk) 13:27, 26 September 2017 (UTC)


 * That works as long as '#' does not appear on non-comment lines. If you get any code line with '#' in it, it will be deleted too. Just apply a regexp that exactly defines what a valid comment is.B8-tome (talk) 13:57, 26 September 2017 (UTC)