-1

What I need is to check if a string only contains the characters > or < or -.

So I thought using a RegEx for this, and I found an SO question with the exact same problem, and it has an answer (not the accepted one but the answer with regex)

This is the SO question : String contains only a given set of characters

So I modified the expression in this question to fit my needs like this :

static readonly Regex Validator = new Regex(@"^[><- ]+$");

and I call it like this ;

Validator.IsMatch(testValue)

But it's throwing the error

x-y range in reverse order

There are lots of question on SO about this error but I cant find or understand the answer I need.

So what am I doing wrong with this RegEx?

GuidoG
  • 11,359
  • 6
  • 44
  • 79

2 Answers2

1

^[-<>]+$ "-" must come first in C# regex

Prodigle
  • 1,757
  • 12
  • 23
  • 1
    Thanks, the answer is simple it was just never explained simple in most so answers – GuidoG Sep 07 '18 at 08:20
  • 1
    `-` symbol has a special meaning and should be escaped when we want to treat it litteraly, e.g. `[0-9]` is a range of characters `0, 1, 2, ..., 9` while `[0\-9]` means exactly `0`, `-` or `9` character. – Crozin Sep 07 '18 at 08:31
0

Escape - within character groups. ([0-9] means "zero to nine" and not "zero, dash or nine")

Paolo
  • 21,270
  • 6
  • 38
  • 69
dognose
  • 20,360
  • 9
  • 61
  • 107