Subhasis
Subhasis

Reputation: 714

regex for four-digit numbers (or "default")

I need a regex for four-digit numbers separated by comma ("default" can also be a value).

Examples:

  1. 6755
  2. 3452,8767,9865,8766,3454
  3. 7678,9876
  4. 1234,9867,6876,9865
  5. default

Note: "default" alone should match, but default,1234,7656 should NOT match.

Upvotes: 0

Views: 8767

Answers (2)

jamessan
jamessan

Reputation: 42667

Based on replies to the comments, it sounds like you need a regular expression for a pattern restriction in an XSD. According to the XSD spec, this should work:

default|[0-9]{4}(,[0-9]{4})*

Upvotes: 1

Greg
Greg

Reputation: 321638

This should do it:

/^(default|\d{4}(,\d{4})*)$/

That's

^         start of string
(         start group
 default  literal "default"
 |        or
 \d{4}    digit repeated 4 times
  (       start group
   ,      literal ","
   \d{4}  four digits
  )       end group
 *        repeat 0 or more times
)         end group
$         end of string

Upvotes: 12

Related Questions