combining subexpressions into 1 Regex expression
-
I want to check for a decimal number where one or more digits are allowed before the decimal point (in which case digits after the decimal point are optional) or one or more characters are allowed after the decimal point (in which case digits before the decimal point are optional). The decimal point is optional, but you cannot have just the decimal point. e.g. 1.3, 1., .1 all ok. . not ok. The pattern @"^\d+\.?\d*$" takes care of the case where there are digits before the decimal point. The pattern @"^\d*\.?\d+$" takes care of the case where there are digits after the decimal point. Either one by itself is not sufficient. I thought I could combine them to have an either/or expression the way you would have [a|n] to say either 'a' or 'n', by making them subexpressions like this: @"[(^\d+\.?\d*$) | (\d*\.?\d+$)]" It doesn't work, though. This matches on each digit and the decimal point. e.g., 30.5 give 4 matches - '3', '0', '.', '5'. I can achieve what I want in other ways, but wondering if someone can tell me if there is a regular expression to do what I want and/or explain why the last pattern matches on each digit - I'm not seeing it. Thanks.