1

I'm refactoring this regular expression C# string into a constant and I'm not sure how best to name it (my regex fu is weak).

@"^(?!\s\s)"

Doesn't the question mark denote something that's optional? What is the question saying is optional?

I've found that, as a developer, you have to clearly understand something before you can properly name it (also: Naming is Hard), so I want to clearly understand this and then give it a proper name.

What is the name of this regular expression?

I'm considering LinesThatDoNotStartWithTwoSpaces or DoesNotStartWithTwoSpaces.

Community
  • 1
  • 1
lance
  • 15,310
  • 17
  • 70
  • 129
  • 5
    I always use this rule: don't refactor things you don't understand. – J. Holmes Apr 11 '12 at 12:15
  • 3
    +1 for giving this much care to a variable name ... a lot of love man .. lot of love! – Galilyou Apr 11 '12 at 12:16
  • I'm not sure what the regex is supposed to do, but the '\s' refers to a whitespace character not a space. It's not clear what the question mark is doing either, as you've also spotted. The '^' character in the context above means 'at the beginning' rather than NOT. – Adrian Thompson Phillips Apr 11 '12 at 12:16
  • 2
    Yes, and that includes linefeeds, carriage returns, and other "vertical" whitespace characters, which is certainly not what you want. To specify as clearly as possible (to human readers as well as to the regex compiler) that you mean only space characters, you can use something like `^(?!\x20\x20)`. – Alan Moore Apr 11 '12 at 13:34

3 Answers3

5

The construct itself is a Negative lookahead. It specifies a group that can not match after your main expression.

In this case, yes, it matches when the line being matched does not start with two spaces (white spaces ).

I would name the regex as NotMatchLineStartingWithTwoWhiteSpaces

manojlds
  • 259,347
  • 56
  • 440
  • 401
1

I always trying to use Not in variable name. For example NotHuman, its meaning will change with the definition of Human. Also, variable name should tells what-this-is but not what-this-can-do, so I think it's not a good idea to use verb in variable name, such as Search, Match.

For your case, how about LineWithoutIndentation, similar to the property text-indent in CSS3.

Byzod
  • 369
  • 3
  • 17
0

It's called Negative lookahead

David Hedlund
  • 121,697
  • 28
  • 196
  • 213
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Crippledsmurf Nov 15 '12 at 10:38
  • @Crippledsmurf: I would've included code if the question was how to write a negative lookahead, but the question contained the negative lookahead already. The question was what the construct was *called*. The name "Negative lookahead", which I've included, *is* the essential part of the answer. The link is just sourcing the claim, I could've written this exact same answer without the link. This answer will not become invalid if the linked page changes. – David Hedlund Nov 15 '12 at 10:41