What is this regular expression's name?

508 views Asked by At

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.

3

There are 3 answers

0
Byzod On BEST ANSWER

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.

2
David Hedlund On

It's called Negative lookahead

0
manojlds On

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