I've tried a lot to find out the value using my regular expression, but somehow its not working, some of you might help.
$102
$102.36
$204.36
I am using below regular expression
[$]\\d+[.]\\d{0,2}
but this is only fetching the value where decimal is present (for my case 102.36 & 204.36), but for my case decimal is optional, please help me out.
Use ? together with parentheses for the optional part:
[$]\\d+([.]\\d{0,2})?
\$([0-9]+(\.[0-9]+)?))
dollar sign followed by a number one or more times optionally followed by (a dot followed by a number 1 or more times)
\$([0-9]+(\.[0-9]{2})?)
Same but will only match 2dp, the first (not 0th) match class will contain the number.
0th match: all of it (inc $)
1st match: all of the number
2nd match: the decimal point and numbers that follow (if present)
to not capture the 2nd match with Perl flavours or Regex:
\$([0-9]+(?:\.[0-9]{2})?)
Related
I want to validate my currency field with regex. I want to allow the following pattern entries
1.23
1
.45
0.56
56.00
No comma should be allowed. I've tried \d+(\.\d\d) but it allows only first, fourth and fifth entries. \d+(?:\.\d\d+)? allows all but third one.
Use \d* instead of \d+ before the decimal to match zero or more digits. Also add anchors (^ and $) or else it will pass as long as there is any match available. This would also validate an empty string, so if necessary you can use a lookahead to make sure there is at least one digit:
^(?=.*\d)\d*(?:\.\d\d)?$
Regexes for floating-point numbers are a solved problem:
\d*\.?\d+
For at least two decimals:
(\d*\.\d)?\d+
To make it more comprehensible:
\d+|\d*\.\d{2,}
And for exactly two decimals:
\d+|\d*\.\d{2}
Depending on your language, don't forget to anchor the expression so that it must match the whole string.
I've come up with this regular expression to validate a number which can have Maximum length-13 (including decimal points),Maximum no of decimal points-3,Maximum length of a whole number-12.
^(\d{1,12}([.]\d{1,1})?|\d{1,11}([.]\d{1,2})?|\d{1,10}([.]\d{1,3})?)$
Could anyone tell me if my approach is correct or give me a better solution?
This would also work:
^(?=.{1,13}$)(\d{1,12})(\.\d{1,3})?$
Uses positive look ahead to match the entire string length is ok.
Then it uses a group to match from 1 - 12 digits
Then there's an optional group to match a decimal followed by 1-3 digits.
Edited: Simplified since the rules don't allow a 13 digit integer-part
I need to figure out how to make my regex allow match correctly each time I type a number/decimal point. I want to limit the number of digits before and after the decimal point, which isnt too hard but i cant figure out how to allow the decimal point to match as well.
1 - match
12 - match
1234 - match
12345 - wrong
1234. - match
1234.1 - match
1234.12 - match
1234.123 - wrong
Other matched numbers
12.12
1.0
123.99
Edit:
So I want a max of 4 numbers before the decimal place and two after. Also the decimal place is optional.
The tricky part is that I want it to fail if the fifth character isn't a decimal point.
You need to specify your constraints better; I'm assuming you want a maximum of 4 before the dot and 2 after:
/^\d{1,4}(\.\d{0,2})?$/
edit: I added beginning and end of string matchers. Should work as you want now
You can use the following regex to select only those words that consists of digits and satisfying your condition.
/(?<=^|\s)\d{1,4}(?:\.\d{0,2})?(?=\s|$)/g
Positive lookahead and lookbehind are used to make sure that a whitespace is around the number.
DEMO
Debuggex Demo
Something like this will help
r'^\d{1,4}(\.\d{0,2})?$'
As you must be aware, \d represents a digit, . for the decimal point and {min_required,max_required}. Be sure to test your regular expression prior to using them here.
I believe I've found the "perfect" Regular Expression to check against for currency in jQuery Validate, except it seems to allow the user to put a single lone decimal at the end or a decimal with a single digit after.
Matches: 700. and 3.0
The Regex:
^\$?([1-9]{1}[0-9]{0,2}(\,[0-9]{3})*(\.[0-9]{0,2})?|[1-9]{1}[0-9]{0,}(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|(\.[0-9]{1,2})?)$
I've been playing with it in http://gskinner.com/RegExr/ but can't seem to modify it in the right places to fix my decimal issue.
Currently it matches everything I need it to:
700,000
700,000.00
700000
Here is the jQuery validator addMethod using #Kolink's regex for those of you curious:
jQuery.validator.addMethod("currency", function(value, element) {
return this.optional(element) || /^\$?(?=.)(?:[1-9]\d{0,2}(?:,?\d{3})*)?(?:\.\d{2})?$/.test(value);
}, "Please enter a valid number.");
In each case your regex is saying "blah blah blah, then optionally a decimal point followed by zero to two digits".
Try this instead:
^\$?(?=.)(?:[1-9]\d{0,2}(?:,?\d{3})*)?(?:\.\d{2})?$
It's a much simpler regex and it does everything your current one does... but better.
Breaking it down:
^ start of string
\$? optional dollar sign
(?=.) require at least one character (because your requirement is for the whole part to be optional, and the decimal to be optional, but requires at least one)
(?:[1-9]\d{0,2}(?:,?\d{3}))*)? optionally match an integer. This integer may or may not be thousand-separated. Must start with a non-zero digit.
(?:\.\d{2})? optionally match a dot followed by two digits.
$ end of string
I would do this:
^\$?(?=.)([1-9]\d{0,2}((,\d{3})*|\d*))?(0?\.\d\d)?$
See a live demo of this working with your examples.
I'm having trouble putting together a regular expression for a string that contains a number between 0 and 99999, followed by a plus sign, followed by one or two digits, optionally followed by a decimal and a single digit. For instance:
99999+99.9
This would also be valid:
0+00
This would also be valid:
0+02.5
This would also be valid:
0+2.5
I found this topic: How can I check for a plus sign using regular expressions?
And this one:
Regular Expression for decimal numbers
But am unable to put the 2 together and fulfill the other requirements listed above.
Any help you can provide is much appreciated!
This should work:
\d{1,5}\+\d{1,2}(?:\.\d)?
\d{1,5} captures anything between 0 and 99999 but also allows zero padding, e.g. 00000 or 00123 (it'll be a little more complicated if you don't want zero padding).
\+ matches a plus sign.
\d{1,2} matches one or two digits.
(?:\.\d) matches a period followed by a single digit. The (?:) bit indicates a non-capture group.
The ? at the end makes the non-capture group optional.
You need to escape the plus and the . -- like so
\d{1,5}\+\d{1,2}\.?\d
Hth!
Here it is
"^[0-9]*([0-9]{0,5}\+[0-9]{1,2}(\.[0-9])?)[0-9]*$"
EDIT: as per you comment, I have modified the expression.