-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
  • 10,365
  • 5
  • 41
  • 71

2 Answers2

1

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

Prodigle
  • 1,638
  • 8
  • 22
  • 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
  • 13,742
  • 6
  • 28
  • 51
dognose
  • 19,568
  • 9
  • 58
  • 104