1

I have a list of specific valid values: XX,SX,FC,SC,Jump.

Basically I need to look at user-supplied list of values and if one of the values does not match the above list I will throw an error. Can I use a regular expression to accomplish this?

2
  • 3
    You're going to want to use the 'split' function of whatever language you're using. And what language is that, by the way? Commented Aug 1, 2011 at 17:47
  • Indeed using split and sets is a way better solution, since that allows you to actually report which values are not allowed. With a regex you can only generate some very unhelpful error message. Commented Aug 1, 2011 at 17:54

1 Answer 1

1

This will match a comma separated list of 5 sequences of alphanumeric characters.

[A-Za-z0-9](,[A-Za-z0-9]){4}

However, and depending on the language you are using, I'd normally split the string and then check the length of the resulting array. For instance, in Java:

String csvList = "XX,SX,FC,SC,Jump";
String[] elements = csvList.split(",");
if (elements.length != 5) {
  throw new Exception();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.