Search code examples
regexgroovy

Regex to match either 0-9 digits of any length or exact string N/A


I have a text field which should be entering either 0-9 characters or exact string N/A. I am using this regex: /[\d]|\bN/A\b/ But this is not matching what I want:

What I Want is:

  1. 123534 - Match

  2. N/A - Match

  3. N/AN/A - no match

  4. N - no match

  5. NA - no match

  6. N/A1234 - no match

  7. 134234N/A - no match

  8. 1234N - no match

My current regex /[\d]|\bN/A\b/ is matching above 6, 7 and 8 case which I don't want.


Solution

  • Here is the regex: ^(\d+|N\/A)$