1

I want to build a regex to search number in a string (using c#) with range from 0000-4095. I use this string pattern:

string regex_pattern = (0?[0-3][0-9][0-9][0-9]|{4}{0}[0-9]{0}|{4}{0}{9}[0-5]);

But i can not get success.

Can you please show me some hints?

Thanks

Dominota
  • 141
  • 7
  • You do not have to post such questions on SO, since you may get all these regexps automatically at http://gamon.webfactional.com/regexnumericrangegenerator/ – Wiktor Stribiżew Feb 21 '20 at 08:12

2 Answers2

3

You could use

^([0123][0-9][0-9][0-9]|40[0-8][0-9]|409[0-5])$

Regex Demo

Sample

enter image description here

Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
  • Thanks @Anu Viswan, your string pattern works fine. Btw, can you show me a hint to match for whole this data protocol like this: "AP n nnnn nn\r\n" .In that string: n is number from 0-9; nnnn is number from 0000-4095; nn is from 0-99; \r\n is CRLF. And there are three space characters – Dominota Feb 21 '20 at 03:18
  • Variations on a theme. Have you tried it yet? – madreflection Feb 21 '20 at 03:22
  • This doesn't work for the entire range. Try for example 4089. You should change the second group to 40[0-8][0-9] – Chronicle Feb 21 '20 at 04:15
  • @Chronicle Good Catch, have updated...Thanks – Anu Viswan Feb 21 '20 at 04:39
  • @Dominota you could use `^AP\s\d\s([0123][0-9][0-9][0-9]|40[0-8][0-9]|409[0-5])\s(\d\d)` with `RegexOptions.Multiline` – Anu Viswan Feb 21 '20 at 04:59
1

sorry for the multiples groups:

([0-4]0(([0-9][0-5])|([0-8][0-9])))|([0-3][0-9]{3})

Sample

EveRegalado
  • 105
  • 1
  • 8