I'm matching whole and fractional numbers with the following pattern:
(0|[1-9][0-9]*)(.[0-9]+)?
It matches for example the numbers:
0
1
12
12.1
12.12
But it also matches:
0.0
0.00
1.00
1.10
My question is how to modify the pattern above to disallow the match of numbers with ending zero after the decimal point.
Your pattern (0|[1-9][0-9]*)(.[0-9]+)? matches all examples because the second part (.[0-9]+)? does not take into account that the last match should be 1-9. Note that you have to escape the dot \. to match it literally.
You could update the second part of your pattern to match 0+ times [0-9]* and make sure that the match ends with [1-9]:
^(0|[1-9][0-9]*)(\.[0-9]*[1-9])?$
Regex demo
The pattern matches:
^ Start of string
(0|[1-9][0-9]*) Capture group, match either 0 or 1 followed by 0+ times 0-9
( Optional capturing group to match
\.[0-9]*[1-9] Match ., then 0+ times 0-9 followed by 1-9
)? Close group
$ End of string
If you are for example not referring to the capturing groups for further processing you might make them non capturing (?: instead.
You can use the following regex:
^(0|[1-9][0-9]*)(.(?:[0-9]+[1-9]|[1-9]))?$
demo: https://regex101.com/r/ZtRg49/5
This will not match at all the following elements:
0.0
0.00
1.00
1.10
If you want to allow partial matches (not take into account the ending 0), you can use:
^(0|[1-9][0-9]*)(.(?:[0-9]+[1-9]|[1-9]))?
demo https://regex101.com/r/ZtRg49/4/
Explanations:
I have added the constraint that after the . there is one digit [1-9] or several digits that can contain 0 followed by a non zero digit [1-9].
By removing the ending anchor $ you enable partial matches
My suggestion to this Problem:
^\d+$|[0-9.]*?[1-9]$
Requirements to be fulfilled:
Match whole numbers
Match floating point numbers not ending on 0
Related
Validate the string using the regex which has the - (hypen)
Requirement is : string contains only digits and - (hyphens) and not end with - (hyphen) and all other digits not be the same.
^([0-9-])(?!\1+$)[0-9-]{5}$
The above one allow only digits and hyphen but its not restricted end with hyphen and check all other digits are same.
ex:
1111-1 Not allowed because all are same digits
1111-2 Allowed
11112- Not allowed as its end with - Hypen
-12345 Not allowed as its start with - hypen
You might write the pattern as
^(\d)(?!(?:\1|-)+$)(?!\d*-\d*-)[\d-]{4}\d$
Explanation
^ Start of string
(\d) Capture a single digit in group 1
(?! Negative lookahead
(?:\1|-)+$ Check that to the right there is not only the group 1 value or hyphens
(?!\d*-\d*-) Assert not 2 hyphens
) Close lookahead
[\d-]{4} Match 4 digits or hyphens
\d Match a digit
$ End of string
Regex demo
If there should be at least 1 hyphen:
^(\d)(?!(?:\1|-)+$)(?=\d*-)[\d-]{4}\d$
Regex demo
My 2 cents to allow [01] hyphens:
^(?=.{6}$)(\d)(?=.*(?!\1)\d)\d+(?:-\d+)?$
See an online demo
I would like to create a regex, that allowes the following patterns:
1234
1234567
123456789
12345678900-
12345678900-123456
It should be possible to only insert numbers and only one hyphen is allowed.
I tried with the following regex:
^[0-9]{1,11}(?(?<=\d{11})[-]?|)[0-9]{6}
It should not be possible to have 11 characters without the hyphen at the end(12345678900 is wrong).
Unfortunatly it didnt work as I intended.
You can match 1-10 digit and optionally match 1 digit followed by - and 6 digits.
^\d{1,10}(?:\d?-(?:\d{6})?)?$
^ Start of string
\d{1,10} Match 1-10 digits
(?: Non capture group
\d?- Match a single optional digit and -
(?:\d{6})? Match optional 6 digits
)? Close non capture group and make it optional
$ End of string
Regex demo
Another variation could be matching 1-10 digits or match 11 digits with a hyphen and optionally 6 digits if the hyphen should only possible after 11 digits.
^(?:\d{1,10}|\d{11}-(?:\d{6})?)$
Regex demo
You can use
^[0-9]{1,11}(?:(?<=[0-9]{11})-(?:[0-9]{6})?)?$
^\d{1,11}(?:(?<=\d{11})-(?:\d{6})?)?$
See the regex demo. Using \d is possible in case it matches ASCII digits in your regex flavor or you do not care if \d matches all possible Unicode digit chars or not.
Details:
^ - start of string
[0-9]{1,11} - one to eleven digits
(?:(?<=[0-9]{11})-(?:[0-9]{6})?)? - an optional occurrence of
(?<=[0-9]{11}) - immediately to the left there must be 11 digits
- - a hyphen
(?:[0-9]{6})? - an optional occurrence of six digits
$ - end of string.
I'm working with a custom credit card validator which has following conditions:
It must start with a 4,5 or 6.
It must contain exactly 16 digits.
It must only consist of digits (0-9).
It may have digits in groups of 4, separated by one hyphen -.
It must NOT use any other separator like ' ' , '_', etc.
It must NOT have 4 or more consecutive repeated digits.
I'm not able to find a regex fulfilling the last condition.
I have following regex for other conditions:
r'[456][0-9]{3}-?[0-9]{4}-?[0-9]{4}-?[0-9]{4}':
For the last condition you could use a negative lookahead that asserts that what follows is not a digit followed by 3 or more times an optional hypen and a digit.
Explanation for the added part (?![-\d]*([0-9])(?:-?\1){3,})
(?!) Negative lookahead
[-\d]* Match zero or more times a digit or a hyphen
([0-9]) Capture a digit in a group
(?:-?\1){3,} Repeat 3 or more times in a non capturing group an optional hyphen -? followed by a backreference to the digit that is captured in group 1
) Close negative lookahead
Your regex with added anchors to assert the start ^ and the end $ of the line could look like:
^(?![-\d]*([0-9])(?:-?\1){3,})[456][0-9]{3}-?[0-9]{4}-?[0-9]{4}-?[0-9]{4}$
I tried different regex which I found here but they are not working.
for example:
1111 = false
1112 = true
It's my homework so I must do it in regex :)
You can use this regex:
^(\d)(?!\1+$)\d{3}$
Explanation:
^ - Match line start
(\d) - match first digit and capture it in back reference #1 i.e. \1
(?!..) is a negative lookahead
(?!\1+$) means disallow the match if first digit is followed by same digit (captured group) till end.
\d{3}$ match next 3 digit followed by line end
How about this?
(?=^\d{4}$)(\d)+(?!\1)\d\d*
The first look-ahead group (?=^\d{4}$) insists that the whole string consists of 4 digits.
The first capture group then matches any number of digits: (\d)+.
After this, there must be a digit is different to the first capture group: (?!\1)\d
Finally, there can be any number of digits trailing: \d*
I am trying enhance the given answer on another post:
^[1-9]\d*(?:\.[05])?$
Breakup:
^ # Start of input
[1-9] # match digit 1 to 9
\d* # match 0 or more of any digits
(?: # start of non-capturing group
\. # match a decimal
[05] # followed by digit 0 or 5
)? # end non-capturing group. (? makes this group optional)
$ # End of input
Only this time the regex needs to accept 0,5 or 0.5 and increments of 0,5 or 0.5
The number zero is the only one that can't be matched.
Already tried:
(?!0).{1}|^[1-9]\d*(?:(,|\.)[05])?$
and
[^0]|^[1-9]\d*(?:(,|\.)[05])?$
Can you help please?
You can use
^(?!0$)\d+(?:[,.][05])?$
See demo
This will match your required numbers and will exclude a 0-only number thanks to the look-ahead at the beginning.
Main changes:
\d+ - replaced [1-9]\d* to allow a 0 as the integer part
[,.] - replace \. to allow a comma as a decimal separator.
The lookahead adds a 0-number exception.
The lookahead can be enhanced to disallow 0.000-like input:
^(?!0+(?:[,.]0+)?$)\d+(?:[,.][05])?$
See another demo.
You can use this regex:
^(?:5|[1-9]\d*[05])(?:[,.][05])?$
RegEx Demo
This should do it:
^(?!0*$)0?\d*([,.]5)?$
See live demo.
This uses ^\d*([,.]5)?$ to match what you want, and uses ^(?!0*([,.]0*)?$) to exclude zero in all its forms, ie it won.t match 0, 000 etc