Domanda

How would one go about writing a data annotation regex for the following?

  • Only digits and periods allowed
  • Format: Block.Block.Block.Block (There must be 4 blocks and 3 periods at all times)
  • Each block can contain up to 4 digits (1 digit at minimum)
  • First block cannot start with a 0
  • If any other block starts with a 0, then no digit can follow

Valid strings:

12.3.4567.0
1.1456.0.8321
4.1200.10.5

P.S. I'm not a Regex guru, and I don't know if the above is doable.

È stato utile?

Soluzione

^[1-9]\d{0,3}(\.(0|[1-9]\d{0,3})){3}$

See here for an annotated explanation of how it works :) http://regex101.com/r/gS4eL0

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top