How do I write a regex that only returns "I" and then "J" or "V" and then between 1 and 3 digits?

An expression is required that only returns things with an "I", followed by either "J" or "V" ("No quotes"), and then at least 1 number up to three numbers.

IJ ###
IV ###
IJ ##
IV ##
I J #
I v #

+1


source to share


4 answers


Depending on your taste

I(J|V)[0-9]{1,3}

      



Do you also need a space after the self?

I (J|V)[0-9]{1,3}

      

+4


source


Your description doesn't match your example and there are some idiomatic things you need to take care of (case insensitive, regex-dependent)

I [JV]\d{1,3}

      

This will match



  • i J1
  • i J12
  • i J123
  • i V1
  • i V12
  • i V123

But there will be no MATCH

  • i 1
  • i am 12
  • I'm 123
+4


source


Tested with RegExBuddy:

I [JV] \ d {1,3} \ s

Edited:

Pretty much like Vinko Vrsalovic, but with it, if you have J12345678, it will take "I J123" in your expression. Adding \ s requires a special trailing char, such as space, string, etc.

+1


source


I think the others missed the spec v#

.

I[JVv]\d{1,3}

      

Of course, the lower case v

was a typo.

0


source







All Articles