2

I have the following right now which works fine if the string is alphanumeric and separated by commas, but it doesn't work if I only have a single string.

"^([a-zA-Z0-9]+,)+[a-zA-Z0-9]+$"

Works for something like "1A,1B,1C", doesn't work on "1A"

Sorry, I don't know much about regex and I only got this through searching.

Here are a few example of what can be valid or invalid

1 - Valid

12 - Valid

1A - Valid

A1 - Invalid

12A - Valid

A12 - Invalid

1AA - Invalid

A - Invalid

1,2,3,6A,6B,11,11A - Valid

Invalid would be if any other characters appear in the string except for numbers and letters also no commas or anything in the prefix and suffix

crimson
  • 221
  • 1
  • 7
  • 25
  • You should tell us what your goal is. What strings do you want to match with? What strings should not match it? – Andrew Shepherd Jun 15 '15 at 03:17
  • @AndrewShepherd mmm... didn't think about looking in to getting a more concrete regex for what I need. My inputs will be a single digit number or 2 digits or a single or 2 digit number with 1 letter. "1, 2, 3, 11, 6A, 6B, 11C, 11D" like those. Inputs can be multiple separated by comma or just one. "11" or "10, 11A, 11B" – crimson Jun 15 '15 at 03:20
  • You could still improve this question by adding some specific non-matching examples. For example, would 'ABCD' match? – Andrew Shepherd Jun 15 '15 at 03:35
  • is you want to say `[any Number] + [ alphanumeric or comma]* +[alphanumeric]` – TeamKhurram Jun 15 '15 at 03:50

2 Answers2

3
^\d{1,2}[A-Z]?(?:,\d{1,2}[A-Z]?)*$

Try this.See demo.

https://regex101.com/r/hI0qP0/25

vks
  • 67,027
  • 10
  • 91
  • 124
1

This regex will work for you Test Given Regex

^\d{1,2}+[a-zA-Z]?+(?:,\d{1,2}+[a-zA-Z]?)*$

Define ^\d{1,2}+[a-zA-Z]?

 \d          : Represent a digit 0-9 
 {1,2}       : Minimum 1 and Maximum 2 Of previous Expressions
 \d{1,2}     : Means 1 to two digits
 +           : Previous Expression Repeats From one to unlimited times
 [a-zA-Z]    : Range Defined for all capital And Small latter [a-z] for small Latter, [A-Z] Capital: latter
 ?           : Repeat Previous Expression 0 or one Times
 [a-zA-Z]?   : One/Zero Charter from "a to z ,A to Z"

^\d{1,2}+[a-zA-Z]? : Start with one or two digits and an optional character

Define (?:,\d{1,2}+[a-zA-Z]?)*$

 ?:          : Non Capturing Group,Match zero to unlimited times//Reference link given Below
 ,           : Match Comma(Given Character must be comma)
 \d          : Represent a digit 0-9 
 {1,2}       : Minimum 1 and Maximum 2 Of previous Expressions
 \d{1,2}     : Means 1 to two digits
 +           : Previous Expression Repeats From one to unlimited times
 [a-zA-Z]    : Range Defined for all capital And Small latter [a-z] for small Latter, [A-Z] Capital: latter
 ?           : Repeat Previous Expression 0 or one Times
 [a-zA-Z]?   : One/Zero Charter from "a to z ,A to Z"
 *           : Match Previous Expression Between zero and unlimited times, as many times as possible
 $           : End of Regex

(?:,\d{1,2}+[a-zA-Z]?)*$ : Start with comma then one or two number/digitd then one character from range a-zA-Z This Expression Match from 0 to ultimate times and the end of regex

^\d{1,2}+[a-zA-Z]?+(?:,\d{1,2}+[a-zA-Z]?)*$ : Start with (one or two) Digit , Followed by one character, Start next expression (one comma 1-2 digits and an optional Character ) and match it 0 to unlimited times as meany times as possible Before End of string

  1. Check online Regex Test
  2. Reference : Non Capturing Group
Community
  • 1
  • 1
TeamKhurram
  • 107
  • 9