2

My editor EditpadPro allows me to create syntax colouring schemes. The scheme I'm writing includes comments which span "--" to end of a line.

I'd like a regex which starts from the "--" but which stops at the last non-blank character. I cannot use "replace" as I'm just entering the regex, not using it myself.

So, if the text in my line of code is:

X=1 -- This is a comment with trailing blanks

Then the regex would return:

-- This is a comment with trailing blanks

The reason for this is that I can highlight the trailing blank space as waste space.

melpomene
  • 84,125
  • 8
  • 85
  • 148
Swifty
  • 71
  • 6

2 Answers2

2

I'm not familiar with EditPad Pro, but

--(?:.*\S)?

might work.

The idea is to match --, followed by 0 or more of any (non-newline) character (.), followed by a non-space character (\S). Because the "0 or more" part is greedy, it will try to match as much of the line as possible, thus making \S match the last non-blank character of the line.

The ? makes the whole thing after -- optional. This is because you might have a comment without a non-space character in it:

--

This should still be matched as a comment, but not any trailing spaces (if any).

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • I don't see why it should be downvoted - it works perfectly fine. – Tim Pietzcker Apr 07 '18 at 14:16
  • Thanks for the suggestions. I'm struggling to understand the `(?:.*\S)` portion of the first one. It's a lookahead, isn't it? But not one I've come across before. I don't recognise the `:` character's function. The second one's explanation made it easy, even for me. But I'd never fit that explanation into the Syntax Coloring Scheme Editor's comment box for the element! – Swifty Apr 07 '18 at 16:42
  • @Swifty: No, it's a [non-capturing group](http://www.regular-expressions.info/brackets.html). It groups the `.*\S` into a single token which is made optional in its entirety by the `?` following it. – Tim Pietzcker Apr 07 '18 at 19:38
  • Thanks for the explanation of non-capturing group. I searched for `regex (?:` last night, and the very first hit said that a novice might confuse this for lookahead. Put's me in my place, then. – Swifty Apr 08 '18 at 06:04
0

In the Syntax Coloring Scheme Editor, use the following regex, making sure that the "Dot all" checkbox is unchecked:

--.*?(?=[^\r\n\S]*$)

Explanation:

--           # Match --
.*?          # Match any number of non-linebreak characters, as few as possible,
(?=          # until the following can be matched from the current position:
 [^\r\n\S]*  # Any number of whitespace characters except newlines
 $           # followed by the end of the line.
)            # End of lookahead

[^\S] is the same as \s, but the negated character class allows you to exclude certain characters from the class of allowed whitespace characters - in this case, newlines.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • I'm also curious. I downvoted sGeeK's "answer", but apparently someone downvoted both of ours, too (in retaliation?). – melpomene Apr 07 '18 at 14:25