I want to build a regex that will extract me numbers from a string. The pattern is
">number<"
Now the number can have decimals or not.
I went with:
"[^\d]+"
This does extract the numbers but because of decimals, it sometimes works bad. Any ideas?
>((\-|\+)?[0-9]+(\.[0-9]+)?)<
Explained:
(\-|\+)? optional sign: - or +
[0-9]+ - non-empty sequence of digits
(\.[0-9]+)? - optional sequence starting with a dot followed by non-empty sequence of digits
> and < at the beginning and at the and (not in the matching group)
In the first matching group you will have your number.
Example here.
Try this (copy with the quotes):
">[0-9]+(\.[0-9]+)?<"
A simple regex which works for integers, floats and negative numbers :
>([\+\-]?\d+\.?\d*)<
The number is in group 1.
If you can use positive lookarounds, this regex matches just a number between >< and nothing else :
(?<=>)[\+\-]?\d+\.?\d*(?=<)
Here in action.
Assuming that it's just the number you want:
(?<=\\>)[0-9]+(\.[0-9]+)?(?=\<)
It matches any number with or without decimals between > and < but excluding > and <
Related
I want to remove the zeros at the end of a number coming after the decimal point. To give an example:
12.009000 should match "000"
I have the regex pattern below but it gives an error A quantifier inside a lookbehind makes it non-fixed width and I can't find any solution to fix that. What is the correct pattern to match successfully?
Pattern: (?<=\.[0-9]*)0+$
With Java, you can do it like this.
(\\d) capture digits
followed by 0's
replace with the captured digits.
$1 is the back reference to the capture group
str = str.replaceAll("(\\.\\d+?)0+$","$1");
System.out.println(str);
Note: It will leave 12.000000 as 12.0.
(\d+[.]?\d*?)0*$
One more step is needed to replace the dot for numbers such as 12.000
Click here for demo: Click Here
Or to deal with numbers such as 12.000 in one step:
(?:(\d+)\.0*$)|(?:(\d+[.]?\d*?)0*$)
Click here for demo: Click Here
Here is my attempt:
(?:[.][0-9]*[1-9])(0+)$|([.]0+$)
This assumes that the input string is actually a number (it won't protect against things like xyz.001). It will not match at all if there are no trailing zeros after decimal point; and if there are, it removes:
sequence of 0s preceded by a [1-9] after [.][0-9]*
or
a [.] followed by a sequence of 0s.
The result will always be in the captured group if the regex matches.
([\d.]+?)(0*)
"Find digits and dots, but not greedily, then find trailing zeros"
Group 1 is the number. Group 2 is the trailing zeros.
I use this regex:
/^(?!0000)(?!0+(?:[.,]\d{1,2})?$)\d{1,4}(?:[.,]\d{1,2})?$/
It allows decimal numbers (. and , separators), with two digits after the separator. It does not allow zero values. What I want to do is to make it allow numbers like 0.1 and 0.09 etc... Here it's impossible to write any number starting with 0. I don't know how to do this. Any idea?
Thanks.
You can simplify your regex with just one negative lookahead:
/^(?![,.0]*$)\d{1,4}(?:[.,]\d{1,2})?$/gm
RegEx Demo
(?![,.0]*$) will prevent any input with just 0s, dots or commas in input.
Any regex based approach would be trivial to bypass. For example, they could use any of the following to evade your regex filter:
+0- Start with +
-0 - Start with -
0e0 - Scientific notation
1e-999 - Not real 0, but most likely will be one after conversion.
And any combination of methods above.
Long story short: regex wouldn't work here.
Try to cast your string to a number and reject if it equals to 0.
You should be able to do that in most languages, including JavaScript.
Since you mentioned JS/Angular, you may replace all , with . (in case the decimal separator is ,) and cast the string to a number to check if it is zero, and if it is, then use your simplified regex /^\d{1,4}(?:[.,]\d{1,2})?$/ that makes sure there are 1 to 4 digits in the whole part and 1 to 2 digits in the fractional part:
function check(str) {
str = str.replace(/,/g, ".");
if ( parseFloat(str) > 0 ) {
return /^\d{1,4}(?:\.\d{1,2})?$/.test(str);
}
return false;
}
console.log(check("0,00"));
console.log(check("0,09"));
console.log(check("900000"));
If you cannot access the code, adjust the negative lookahead like
^(?!0+(?:[.,]0+)?$)\d{1,4}(?:[.,]\d{1,2})?$
See the regex demo
The (?!0+(?:[.,]0+)?$) negative lookahead fails the match only if the string starts with 1 or more zeros, and then has an optional sequence of , or . followed with 1 or more zeros.
^ - start of a string
(?!0+(?:[.,]0+)?$) - reject the match if it matches a sequence of
0+ - 1 or more zeros
(?:[.,]0+)? - an optional sequence of . or , followed with 1 or more zeros
\d{1,4} - 1 to 4 digits
(?:[.,]\d{1,2})? - an optional sequence of:
[.,] - a . or ,
\d{1,2} - any 1 or 2 digits
$ - end of string.
The lookahead pattern may be reduced to (?!0*[.,]?0+$), since the consuming pattern will ensure the correct format is matched.
I want to validate number that don't contain the minus char. The number > 0.
Have you got a regex for that ?
Exclusivly non-negativ numbers with decimal-point: ^\d+(?:.\d+)?$, or capturing with negativ look-behind ((?<!-)[[:digit:]]+) or a myriad other ways depending on the flavour of regex you need and the real problem at hand.
To match absolutes
^\d+$
https://regex101.com/r/O4nGl5/2
To match decimals
^\d+(\.?\d+)?$
https://regex101.com/r/O4nGl5/3
There multiple ways to do that, one of them is:
^[0-9]+$ (for integer numbers)
It checks your input against:
Starts and ends with an integer (^ for beginning and $ for end)
characters between between 0 and 9 (integers)
1 or more occurence (+ for 1 or more occurrences of the previous expression)
I want except some numbers in different syntax and I am trying to find the best Regex for this task/match.
First some valid numbers:
0.01
0.2
0.38
45
165.6
52732.08
999999999.99
And here some invalid numbers:
.01
.2
.50
.85
45.
45.0
45.00
00045.0
124.60
000124.60
124,6
000053853.01
999.999.999,99
999999999,99
After several tests I have created the following Regex:
^[1-9]?\d{1,9}\.?\d{1,2}(?<!0)$
But I always struggling on the number: 000058723.01
Any ideas? Thanks.
You can use this regex:
^(?!0+\d)\d+(?:\.(?![1-9]*0+$)\d{1,2})?$
Or:
^(?:0+|[1-9]\d*)(?:\.(?![1-9]*0+$)\d{1,2})?$
RegEx Demo
Try this pattern:
^((?:0|[1-9]+)(?:\.(?:\d+?[1-9]|[1-9]))?)$
Demo
You accept four kinds of input:
A number with no decimal places and without leading zeroes: [1-9]\d*
Zero followed by a dot followed by digits (without trailing zeroes): 0\.\d*[1-9]
A decimal number without leading or trailing zeroes: [1-9]\d*\.\d*[1-9]
Zero: 0
Putting the four together:
^([1-9]\d*|0\.\d*[1-9]|[1-9]\d*\.\d*[1-9]|0)$
Here is a fixed version of your regex:
^(?!0{2,})\d+(?:\.\d{1,2}(?<!0))?$
Here, initial 2 or more zeros are not allowed with the lookahead (?!0{2,}), and the decimal part is made optional within a non-capturing group (?:\.\d{1,2}(?<!0))?.
See demo
In case you do not want to match 0, you can exclude this in the negative lookahead:
^(?!0{2,}|0$)\d+(?:\.\d{1,2}(?<!0))?$
^^
See Demo 2
A number with optional decimals is composed from two pieces: the integer part and the optional decimal part that starts with a dot.
The integer part is either zero (0) or a sequence of digits that start with 1..9 (no 0) and can continue with zero or more digits:
0|[1-9][0-9]*
If you need to impose an upper limit on the integer part's length then replace * with {,n} where n is the maximum allowed length minus 1.
The decimal part starts with a dot (.) followed by zero or more digits and followed by one of 1..9 (no 0 allowed at the end).
The expression is:
\.[0-9]*[1-9]
Now let's combine them:
^(0|[1-9][0-9]*)(\.[0-9]*[1-9])?$
What I added when I joined the pieces:
^ - match the start of the string; without this the regex matches 45.0 from 00045.0;
parentheses around the integer part because of the lower precedence of |;
parentheses around the decimal part, followed by ? to signal the entire decimal part is optional;
$ - match the end of the string to avoid matching 124.6 from 124.60.
Remarks
The above regex was designed to match your examples. However, please notice that most programming languages allow most or all of the numbers you put in the "invalid" section and use a dot (.) as decimal separator. And many languages provide library functions that are able to parse the numbers that use a comma (,) as decimal separator.
Numbers without integer part (.85), without digits after the dot (45.) ore with trailing zeros (45.0) are valid and are interpreted without ambiguity.
The only troublemaker is the leading zero (00045.0). For integer numbers, most of the times it is a signal that the number is represented in base 8 while for real numbers it is simply ignored.
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.