-1

I use this regular expression for checking

public const string FullNameRegularExpression = @"^[a-zA-Z0-9._-]+$";

How to add "spacebar" in?

Heidel
  • 3,174
  • 16
  • 52
  • 84

3 Answers3

4

If you are looking for one single space it is: (" "), a very complete example can be found in this reference.

Or if you want to match any whitespace character (\n,\r,\f,\t, ), you can use \s.

Community
  • 1
  • 1
Theolodis
  • 4,977
  • 3
  • 34
  • 53
0

Notice an added \s

public const string FullNameRegularExpression = @"^[a-zA-Z0-9._-\s]+$";
Smile
  • 2,770
  • 4
  • 35
  • 57
  • that would also match \n,\r,\f,\t..OP needs to match only space!.. – Anirudha Oct 07 '13 at 08:50
  • 1
    @Anirudh: That is not an issue here as you'll not have tab or newline etc in a filename anyways ;-) – Artur Oct 07 '13 at 08:51
  • @Artur Then why use a selector that matches them? – JJJ Oct 07 '13 at 08:54
  • @Juhana: why use a dot in a regex to match anything then? ;-) – Artur Oct 07 '13 at 08:56
  • @Artur: You got that wrong with the dot. See my comment to your answer. – Daniel Hilgarth Oct 07 '13 at 09:00
  • @Artur That's really not the same thing, is it? If you have a filename that's *guaranteed* to not contain any other whitespace than space, there's no reason to use any other selector than the space character. But there are valid reasons to use `.` to match everything instead of typing out each possibility (which would be somewhere around a hundred in case of ASCII filenames, not to mention Unicode strings). – JJJ Oct 07 '13 at 09:02
  • People, please realize: The dot *inside a character expression* does **not** match everything. It matches a dot. Nothing more. – Daniel Hilgarth Oct 07 '13 at 09:04
  • @DanielHilgarth I thought we were talking about a general case, not this regex specifically. – JJJ Oct 07 '13 at 09:05
-1

You may push a spacebar on your keyboard or add \s or \s+ or \s* to your regex ;-)

Artur
  • 7,038
  • 2
  • 25
  • 39