Regular expression for checking comma numbers in Flex

Can someone help me find a suitable regular expression for checking a line with comma numbers, for example. '1,2,3'or '111,234234,-09'etc. Everything else should be considered invalid. eg. '121as23'or '123-123'is invalid.

I assume this should be possible in Flex using a regular expression, but I cannot find the correct regular expression.


@Justin, I tried your suggestion /(?=^)(?:[,^]([-+]?(?:\d*\.)?\d+))*$/, but I ran into two problems:

  • This will lead to invalidity '123,12', which must be true.
  • This will not result in invalidation '123,123,aasd', which is invalid.

I tried another regex - [0-9]+(,[0-9]+)*which works quite well, except for one problem: it checks '12,12asd'. I need something that will only allow numbers separated by commas.

+3
source share
2 answers

Your example data consists of three decimal integers, each of which has an optional plus or minus sign, separated by commas without spaces. Assuming this describes your requirements, the Javascript / ActionScript / Flex regex is simple:

var re_valid = /^[-+]?\d+(?:,[-+]?\d+){2}$/;
if (re_valid.test(data_string)) {
    // data_string is valid
} else {
    // data_string is NOT valid
}

However, if your data can contain any number of integers and can have spaces, the regex gets a little longer:

var re_valid = /^[ \t]*[-+]?\d+[ \t]*(,[ \t]*[-+]?\d+[ \t]*)*$/;

( , ..), CSV, .

+2

, :

/(?!,)(?:(?:,|^)([-+]?(?:\d*\.)?\d+))*$/

Flex, / regex Flex. 1. (?:\d*\.)?, .

:

(?!,)         #Don't allow a comma at the beginning of the string.
(?:,|^)       #Your groups are going to be preceded by ',' unless they're the very first group in the string. The '(?:blah)' means we don't want to include the ',' in our match groups.
[-+]?         #Allow an optional plus or minus sign.
(?:\d*\.)?\d+ #The meat of the pattern, this matches '123', '123.456', or '.456'.
*             #Means we're matching zero or more groups. Change this to '+' if you don't want to match empty strings.
$             #Don't stop matching until you reach the end of the string.
+3

All Articles