I am using this regular expression to match 8 digits signed floating point number.
string exp= "12345678";
string regEx1="^([-+]?[(\\d+\\.?(\\d+)?)]{1,8})($)";
Regex topRowRegx = new Regex(regEx1, RegexOptions.IgnoreCase | RegexOptions.Multiline);
Match matchResult = topRowRegx.Match(exp.Trim());
irrespective of -/+ and . symbols it should match 1 to 8 digits number.
It should match -1.2345678, 123.45678, +12.34, 1.2, 1, 12345678, 1254.
There should be at least one digits before decimal and after decimal, if decimal symbol presents.
The above expression working fine but it is failing when I use -/+ or . with 8 digit number.
Can you help me how to identify exactly 8 digits and leave remaining symbols count?
UPDATE:
Vasili Syrakis answer solved the above problem. Just for curiosity, why this is not giving correct result?
string exp = "text> -9.9999999 \"some text here\"";
var resultNumber = Regex.Match(exp, "[-+]?(\\d{1,8}|(?=\\d\\d*\\.\\d+?$)[\\d\\.]{1,9})");
("Result:"+resultNumber.ToString()).Dump();
Altered regex:
^[-+]?(\d{1,8}|(?=\d\d*\.\d+?$)[\d\.]{1,9})$
Escaped version:
^[-+]?(\\d{1,8}|(?=\\d\\d*\\.\\d+?$)[\\d\\.]{1,9})$
Explanation
It will either find an 8 digit number
OR it will find 9 instances of either a period or number... ONLY if there's 1 period separating the numbers. The 9 is to account for the period.
Online demo
http://regex101.com/r/kD1oT6
Try this regex:
^[+-]?(?:(?=\d+\.\d+$)[\d.]{3,9}|(?=\d+$)\d{1,8})$
Basically it has two regex are OR'ed together. First one is checking for pattern line xx.xx, means digits at the both side of the dot. Which means it can have minimum 3 to maximum 9 in length.
Second one is trying to match the digits xxxx in format. Which means it can have 1 to 8 in length.
You can get more explanation of this regex from this link.
Related
I'm trying to extract a software version (pure numbers and decimals) from a text string in a cell, but because it has multiple decimals places I can't get the full result.
Examples (Input --> Output):
Plugin Version v4.5.2 Available --> 4.5.2
New Plugin v1.15.49 Available --> 1.15.49
So far I'm working with this formula, but it only gives me the first decimal result, it can't handle 2 decimals because these are software version numbers, not real numbers.
=REGEXEXTRACT(A1,"-*\d*\.?\d+")
You can also try
=regexextract(A1; "[0-9.]+")
Try like:
=REGEXEXTRACT(A1;"(-*\d*[\.?\d+]+)")
Explanation:
The original:
-*\d*\.?\d+
matches:
-*: 0 to n - characters followed by:
\d*: 0 to n decimal characters (0-9), followed by:
\.?: 0 to 1 . character(s) (it has to be escaped, otherwise it means "any character"), followed by:
\d+: 1 to n decimal characters.
We now:
wrap \.?\d+ into a "selection" ([...])
and match 1 to n(+) of its occurences: [\.?\d+]+
additionally(not mandatory) enclose all in a "capturing group" ((...)) ...we could also: extract parts of it.
sample sheet
https://www.google.com/search?q=regex+tutorial
If you just want the number after v,
=REGEXEXTRACT(A1,"v([\d\.]+)")
\d any digit
\. literal .
[]+ match one or more of any of the characters inside []
Regular expression in java for a string which can contain 2 digits & 10 alphabets irrespective of their position in String
Examples of string are:
1abcdefghij2
12abcdefghij
abcdefghij12
abcdefg1hij2
ab12cdefghij
Is it possible?
I think the regex you are looking for is like this.
Regex: ^(?=\D*\d\D*\d\D*$)[a-zA-Z0-9]{12}$
Explanation:
(?=\D*\d\D*\d\D*$) checks for presence of 2 digits.
[a-zA-Z0-9]{12} makes sure that the total length is 12.
Since presence of 2 digits is already checked obviously there will be 10 alphabets.
Regex101 Demo
Edit #1: Edited regex on Sebastian Proske's advice from
^(?=.*\d.*\d)[a-z0-9]{12}$ to ^(?=\D*\d\D*\d\D*$)[a-zA-Z0-9]{12}$
Yes, it's possible.
[12a-j]+ for strings not limited by length and [12a-j]{12} for string exactly 12 characters long.
You can test it here.
I'm trying to match numbers greater than 40. The good point is that all of them have 2 decimal places, so all of them are like: 3.25, 5.89, 999.75 and they don't use any leading zeros (except on the decimal part that always have 2 digits)...
At first I tried the following code but then I realized this wouldn't match numbers like 100, 1000... even if they are greater than 40.
[4-9][0-9]\.
I don't have to match the decimal part, so don't worry about matching that, just help me to find how to match numbers greater than 40 (up to 9999 would be fine).
Thanks for your help.
This should do the job:
([4-9][0-9]|\d{3,})\.
Check it here:
http://www.regexr.com/3a5v9
Don't use regular expressions for number comparison. If, for example, you're using Javascript:
var aNumber = parseFloat("50");
if (aNumber > 40) {
// yay!
}
If your regex flavour can use negative lookbehind to match the numbers from 41 to 9999 without decimal:
\b(?:[1-9][0-9]{2,3}|[5-9][0-9]|4[1-9])(?<!\.\d{1,2})\b
(40\.(?!0[^\d]|00)\d{1,2}|(((4[1-9](?!\d)|[5-9][0-9])(?![\d])|\d*[1-9]\d{2,})(\.\d{1,2})?))
This prevents false positives from leading 0s.
This worked for me.
It tries to match 40 followed by 1 or two decimals that are not 00.
It then tries to match 4 followed by 1-9, decimal optional.
If it can't match that it matches 5-9 followed by 0-9, decimal optional.
It then triese to match any digit, any number of times, followed by 1-9, followed by 1 or 2 digits, decimal optional.
If you want to require the decimal, just remove the last question mark.
This will do it:
([4-9][0-9]+|\d{3,})
This it will get all the numbers of two digits having the first one greater than 4 or any number with three digits.
As an example http://www.regexr.com/3a5v0
You can use brackets to indicate a minimum and, if desired, maximum number of characters to match. So,
([4-9][0-9]|[1-9][0-9]{2,})\.
matches 4-9 followed by one or more digits. Presumably there's a boundary of some sort at the beginning of this, but it sounds like you have that part worked out. This uses an OR to allow for two possible groups of first digits.
(Most of the other answer are perfect for me -- This is paranoia and a bad idea :)
for use with grep -Po or Perl we could use:
'\b(\d{3,}|[4-9]\d)\.\d\d'
but this would get 40.00 (not greater than 40)
'\b(\d{3,}|[5-9]\d|4[1-9])\.\d\d|\b40\.\d?[1-9]\d?'
Corresponding to:
DDD.DD
| [5-9]D.DD
| 4[1-9].DD
| 40.D[1-9]
| 40.[1-9]D
In flex(1) you have this code to parse strings and get numbers greater than 40:
pru.l:
%option noyywrap
%%
\+?(0*[4-9][0-9]|0*[1-9][0-9][0-9][0-9]*)(\.[0-9]*)? { printf("Greater than 40: %s\n", yytext); }
\-?[0-9]*(\.[0-9]*)? { printf("Lesser than 40: %s\n", yytext); }
\n |
. ;
%%
int main()
{ yylex(); }
Install flex and compile this file it with
make pru
Then run it as:
pru <filein >fileout
or just
pru
This code constructs a deterministic finite automaton from the regular expressions listed and prints the commands listed on the right when recognizes a value greater than 40. It allows a leading optional sign and leading zeros, and an optional fractional part composed of any number of digits. And it does this with only one asignment and one decision for each character read. You have access to the automaton state table generated by flex (it writes C code for you)
the regex that recognizes numbers greater than 40 (with decimals and leading sign and zeros) is:
\+?(0*[4-9][0-9]|0*[1-9][0-9][0-9][0-9]*)(\.[0-9]*)?
and can be abreviated as:
\+?(0*[4-9][0-9]|0*[1-9][0-9]{3,})(\.[0-9]*)?
explanation:
\+? matches an optional plus sign.
(...|...) two options:
0* optional arbitrary number of leadin zeros.
[4-9][0-9] the numbers 40 to 99
[1-9][0-9]{3,} the numbers 100 and up.
(.[0-9]*)? optional decimal point followed by an arbitrary number of digits.
Need regular expression which have:
Maximum 8 digits before decimal(.) point
Maximum 4 digits after decimal point
Decimal point is optional
Maximum valid decimal is 8 digits before decimal and 4 digits after decimal
So 99999999.9999
The regular rexpression I have tried ^\d{0,8}[.]?\d{1,4}$ is failing for 123456789
and more than this. means it is taking more than 8 digits if decimal point is not available.
Tested here : http://regexpal.com/
Many many thanks in advance!
^\d{0,8}(\.\d{1,4})?$
You can make the entire decimal optional
You can try this:
^\d{1,8}(?:\.\d{1,4})?$
or
^[1-9]\d{0,7}(?:\.\d{1,4})?$
If you don't want to have a zero as first digit.
You can allow this if you want: (.1234)
^[1-9]\d{0,7}(?:\.\d{1,4})?|\.\d{1,4}$
Any of the above did not work for me.
Only this works for me
^([0-9]{0,2}((.)[0-9]{0,2}))$
This regex is working for most cases even negative prices,
(\-?\d+\.?\d{0,2})
Tested with the following,
9
9.97
37.97
132.97
-125.55
12.2
1000.00
10000.00
100000.00
1000000.00
401395011
If there is a price of $9.97, £9.97 or €9.97 it will validate 9.97 removing the symbol.
1-(\$+.[1-9])
2-(\£+.[1-9])
You can use this expression for complete price digits.
I'm using this:
^[1-9]\d{0,7}(\.\d{1-4})$
^ = the start of the string
[1-9] = at least the string has to begin with 1 number between 1 and 9
\d{0,7} = optional or max 7 times d (digit: a number between 0 and 9)
() = create a group like a substring
. = need a .
\d{1-4} = digit repited max 4 time
$ end of the string
For price validation we can not allow inputs with leading repeating zeros like 0012 etc.
My solution check for any cases. Also it allows maximum 2 decimal point after the dot.
^(?:0\.[0-9]{1,2}|[1-9]{1}[0-9]*(\.[0-9]{1,2})?|0)$
I am having a bit of difficulty with the following:
I need to allow any positive numeric value up to four decimal places. Here are some examples.
Allowed:
123
12345.4
1212.56
8778787.567
123.5678
Not allowed:
-1
12.12345
-12.1234
I have tried the following:
^[0-9]{0,2}(\.[0-9]{1,4})?$|^(100)(\.[0]{1,4})?$
However this doesn't seem to work, e.g. 1000 is not allowed when it should be.
Any ideas would be greatly appreciated.
Thanks
To explain why your attempt is not working for a value of 1000, I'll break down the expression a little:
^[0-9]{0,2} # Match 0, 1, or 2 digits (can start with a zero)...
(\.[0-9]{1,4})?$ # ... optionally followed by (a decimal, then 1-4 digits)
| # -OR-
^(100) # Capture 100...
(\.[0]{1,4})?$ # ... optionally followed by (a decimal, then 1-4 ZEROS)
There is no room for 4 digits of any sort, much less 1000 (theres only room for a 0-2 digit number or the number 100)
^\d* # Match any number of digits (can start with a zero)
(\.\d{1,4})?$ # ...optionally followed by (a decimal and 1-4 digits)
This expression will pass any of the allowed examples and reject all of the Not Allowed examples as well, because you (and I) use the beginning-of-string assertion ^.
It will also pass these numbers:
.2378
1234567890
12374610237856987612364017826350947816290385
000000000000000000000.0
0
... as well as a completely blank line - which might or might not be desired
to make it reject something that starts with a zero, use this:
^(?!0\d)\d* # Match any number of digits (cannot "START" with a zero)
(\.\d{1,4})?$ # ...optionally followed by (a decimal and 1-4 digits)
This expression (which uses a negative lookahead) has these evaluations:
REJECTED Allowed
--------- -------
0000.1234 0.1234
0000 0
010 0.0
You could also test for a completely blank line in other ways, but if you wanted to reject it with the regex, use this:
^(?!0\d|$)\d*(\.\d{1,4})?$
Try this:
^[0-9]*(?:\.[0-9]{0,4})?$
Explanation: match only if starting with a digit (excluding negative numbers), optionally followed by (non-capturing group) a dot and 0-4 digits.
Edit: With this pattern .2134 would also be matched. To only allow 0 < x < 1 of format 0.2134, replace the first * with a + above.
This regex would do the trick:
^\d+(?:\.\d{1,4})?$
From the beginning of the string search for one or more digits. If there's a . it must be followed with atleast one digit but a maximum of 4.
^(?<!-)\+?\d+(\.?\d{0,4})?$
The will match something with doesn't start with -, maybe has a + followed by an integer part with at least one number and an optional floating part of maximum 4 numbers.
Note: Regex does not support scientific notation. If you want that too let me know in a comment.
Well asked!!
You can try this:
^([0-9]+[\.]?[0-9]?[0-9]?[0-9]?[0-9]?|[0-9]+)$
If you have a double value but it goes to more decimal format and you want to shorter it to 4 then !
double value = 12.3457652133
value =Double.parseDouble(new DecimalFormat("##.####").format(value));