How to write a regular expression to validate a variable against 0-100 or an e - regex

I would like to write a regular expression to validate and input field against the following arguments:
field is required (cannot be
empty)
field must not be a negative number
field must be a validate decimal
number to two decimals (eg. 1 or 1.3
or 1.23)
field can be any valid number between 0 and 100 or an 'e'

Regular expressions find great use in checking format, but you're wishing to use it to do a subset of floating point number parsing and bounds checking. Be kind to yourself and the person who will maintain your code after you're gone: check if it's an 'e', else read it into a float and check the bounds.

You can use: ^(100|\d{1,2}(\.\d{1,2})?|e)$
However, it would be simpler and more readable to use your language's float parsing/casting functions.
EDIT: Some variations based on the comments:
Allowing 100.0 and 100.00: ^(100(\.0{1,2})?|\d{1,2}(\.\d{1,2})?|e)$
Disallowing leading zeroes: ^(100(\.0{1,2})?|[1-9]?\d(\.\d{1,2})?|e)$

^(?:100|\d{1,2}(?:\.\d{1,2})?|e)$

Hmm does this work for you?
^((100|[0-9]{1,2})(\.[0-9]{1,2})?)|(e)$
Whay environment is this for? Any particular regex standard it must adhere to?
Constraints on numeric values (such as "> 100", or "<= 5.3") can make regexes rather complicated. These types of contraints are better checkedin application logic. Then you can have a simpler (and easier to understand) pattern:
^(([0-9]{1,3})(\.[0-9]{1,2})?)|(e)$
And then extract the capture group for the first 3 digits and validate that separately.
Edit:
Ok I think this one should do it (last one because my eyes are getting tired):
^(100(\.0{1,2})?)|([0-9]{1,2})(\.[0-9]{1,2})?|(e)$
Will also allow 100.00 or 100.0

Related

Is there a simple way to replace non numeric characters hive excluding - to allow only -ve and +ve numbers

The following will give me 9090 but I wish to get -9090
regexp_replace('abcd-9090',[^0-9],'')
If I use regexp_replace('abcd-9090',[^0-9-],'')
then it gives -9090
but when the string is abcd9090- it would give me 9090-
There could be many more cases I guess where abc-abcd-9090 would give me -9090 but its safe to assume that such will not be the case and there would be only a single - before the numeric values.
Since there could be many cases , I am just supposed to assume the best and replace the flawed code with a more correct pattern which produces an integer almost always.
May be like assuming a condition where only single - could come at the beginning of any digits in the string is okay to assume.
Any help is appreciated.
I guess you can try to use regexp_extract instead:
regexp_extract('abcd-9090','.*(-[0-9]+)',1)
UPD from comment - author need to address one more corner case:
regexp_extract(regexp_replace('-ab2cd9090','[^\\d-]+',''),'(-?\\d+)',1)

Validate a string containing 1, 2, 3, or 4 fields?

I need some help building a regular expression for a string which may contain 1, 2, 3, or 4 fields. Each field has a format of: tag=value.
Below is a comprehensive list of all possible strings I can have. code tag is a three-digits number:
type=buy&code=123&time=yes&save=yes
type=buy&code=123&time=yes&save=no
type=buy&code=123&time=no&save=yes
type=buy&code=123&time=no&save=no
type=buy&code=123&time=yes
type=buy&code=123&time=no
type=sell&code=123&time=yes&save=yes
type=sell&code=123&time=yes&save=no
type=sell&code=123&time=no&save=yes
type=sell&code=123&time=no&save=no
type=sell&code=123&time=yes
type=sell&code=123&time=no
type=long&code=123
type=short&code=123
type=fill&code=123
type=confirm&code=123
type=cancelall
type=resendall
So these are the possible values for the four tags:
type={buy|sell|long|short|fill|confirm|cancelall|resendall}
code=[[:digit:]]{3}
time={yes|no}
save={yes|no}
This is what I have right now:
value={buy|sell|long|short|fill|confirm|cancelall|resendall}&code=[[:digit:]]{3}&time={yes|no}&save={yes|no}
It is obviously not correct, I do not know how make number of fields to be variable.
I want to use regular expression to check if the string is in correct format from C++ code. I am already doing it by parsing the string and using multiple "if" statements which makes tens of lines of code and it is also error prone.
Thank you!
This regex will do it:
/^type=(?:(?:buy|sell)&code=\d{3}&time=(?:yes|no)(?:&save=(?:yes|no))?|(?:long|short|fill|confirm)&code=\d{3}|cancelall|resendall)$/
(using two anchors, an optional item and lots of alternations in non-capturing groups)
I am already doing it by parsing the string and using multiple "if" statements
For checking rules, this might be the better alternative. You still might use regexes for tokenizing your string.
You also might want to have a look at a parser generator, since you already seem to have a grammar available. The generator will yield parser code from that, which can be called to check the validity of your inputs and will return helpful error messages.

Regex less than or greater than 0

I'm trying to find a regex that validates for a number being greater or less than 0.
It must allow a number to be 1.20, -2, 0.0000001, etc...it simply can't be 0 and it must be a number, also means it can't be 0.00, 0.0
^(?=.*[1-9])(?:[1-9]\d*\.?|0?\.)\d*$
tried that but it does not allows negative
I don't think a regex is the appropriate tool for that problem.
Why not using a simple condition ?
long number = ...;
if (number != 0)
{
// ...
}
Why using a bazooka to kill a fly ?
also tried something:
-?[0-9]*([1-9][0-9]*(\.[0-9]*)?|\.[0-9]*[1-9][0-9]*)
demo: http://regex101.com/r/bZ8fE5
Just tried something:
[+-]?(?:\d*[1-9]\d*(?:\.\d+)?|0+\.\d*[1-9]\d*)
Online demo
Take a typical regex for a number, say
^[+-]?[0-9]*(\.[0-9]*)?$
and then require that there be a non-zero digit either before or after the decimal. Based on your examples, you're not expecting leading zeros before the decimal, so a simple regex might be
^([+-]?[1-9][0-9]*(\.[0-9]*)?)|([+-]?[0-9]*\.0*[1-9]*0*)
Then decide if you still want to use a regex for this.
Try to negate the regex like this
!^[0\.]+$
If you're feeling the need to use regex just because it's stored as a String you could use Double.parseDouble() to covert the string into a numeric type. This would have an added advantage of checking if the string is a valid number or not (by catching NumberFormatException).

Can someone provide a regex for validating and parsing a csv of integers and reals

I am new to regex and struggling to create an expression to parse a csv containing 1 to n values. The values can be integers or real numbers. The sample inputs would be:
1
1,2,3,4,5
1,2.456, 3.08, 0.5, 7
This would be used in c#.
Thanks,
Jerry
Use a CSV parser instead of RegEx.
There are several options - see this SO questions and answers and this one for the different options (built into the BCL and third party libraries).
The BCL provides the TextFieldParser (within the VisualBasic namespace, but don't let that put you off it).
A third party library that is liked by many is filehelpers.
Using REGEX for CSV parsing has been a 10 year jihad for me. I have found it remarkably frustrating, due to the boundary cases:
Numbers come in a variety of forms (here in the US, Canada):
1
1.
1.0
1000
1000.
1,000
1e3
1.0e3
1.0e+3
1.0e+003
-1
-1.0 (etc)
But of course, Europe has traditionally been different with regard to commas and decimal points:
1
1,0
1000
1.000e3
1e3
1,0e3
1,0e+3
1,0e+003
Which just ruins everything. So, we ignore the German and French and Continental standard because the comma just is impossible to work out whether it is separating values, or part of values. (The Continent likes TAB instead of COMMA)
I'll assume that you're "just" looking for numerical values separated from each other by commas and possible space-padding. The expression:
\s*(\-?\d+(?:\.\d*)?(?:[eE][\-+]?\d*)?)\s*
is a pretty fair parser of A NUMBER. Catches just about every reasonable case. Doesn't deal with imbedded commas though! It also trims off spaces, either side of the number.
From there, you can either build an iterative CSV string decomposer (walking each field, absorbing commas, assigning to an array, say), or use the scanf type function to do the same thing. I do prefer the iterative decomposition method - as it also allows you to parse out strings, hexadecimal, and virtually any other pattern you find in the data.
The regex you want is
#"([+-]?\d+(?:\.\d+)?)(?:$|,\s*)"
...from which you'll want capture group 1. However, don't use regex for something like this. String manipulation is much better when the input is in a very static, predictable format:
string[] nums = strInput.split(", ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
List<float> results = (from n in nums
select float.Parse(n)).ToList();
If you do use regex, make sure you do a global capture.
I think you would have to loop it to check for an unknown number of ints... or else something like this:
/ *([0-9.]*) *,? *([0-9.]*) *,? *([0-9.]*) *,? *([0-9.]*) *,? *([0-9.]*) */
and you could keep that going ",?([0-9]*)" as far as you wanted to, to account for a lot of numbers. The result would be an array of numbers....
http://jsfiddle.net/8URvL/1/

How to validate with regex that a string is OK as long as it contains 10 digits?

I'm processing input from a Web form. Basically, all I care about is that the value provided includes 10 digits, no more, no less.
These would be valid inputs:
1234567890
123 456 789 0 Hello!
My number is: 123456-7890 thanks
These would be invalid inputs:
123456789033 (too long)
123 Hello! (too short)
My number is one five zero nine thanks (no digits)
I've tried many different things with Regextester but it never matches correctly. I'm using the 'preg' setting (which is what I figured my CMS Typo3 uses) and my closest attempt is:
([0-9][^0-9]*){10}
which is kinda lame but is the closest I got.
Cheers!
EDIT: I cannot use any programming language to implement this. Imagine that I have a admin console field in front of me, in which I must enter a regular expression that will be used to validate the value. That's all the latitude I have. Cheers.
I think you've got the right idea. Maybe you can simplify it as (\d\D*){10}
If the regex has to match the complete string, you would want \D*(\d\D*){10}
UPDATE: It looks like you need ^\D*(\d\D*){10}$ to make sure you match the complete string.
A regular expression is not always the best tool for this kind of job. In this case it's probably easier and simpler to write a function to count the number of digits in a string. (Since you didn't mention a programming language, I'll use Python in my example.)
def count_digits(s):
return len([x for x in s if x.isdigit()])
Then, you can use it like this:
s = "My number is: 123456-7890 thanks"
if count_digits(s) == 10:
print("looks okay")
else:
print("doesn't contain 10 digits")