I am trying the following Regex and It is failing
/^\d{1,18}[.]?$/
I want digit 1-18 but a optional dot(.) anywhere. I tried the following too
/^[1-9]{1,18}[.]?$/
It counts . as a character as well i.e 12345678901234567.
How can I achieve 18 digits and an optional . anywhere in regex
You may use this regex with a lookahead to block 2 dots:
^(?!(?:\d*\.){2})[.\d]{1,18}$
RegEx Demo
RegEx Details:
^: Start
(?!(?:\d*\.){2}): Negative lookahead to disallow 2 dots
[.\d]{1,18}: Match dot or digit for length range between 1 to 18
$: End
You may use
^(?=(?:\.?\d){1,18}\.?$)\d*\.?\d*$
See the regex demo
Details
^ - start of string
(?=(?:\.?\d){1,18}\.?$) - a positive lookahead that requires 1 to 18 occurrences of an optional . and any digit followed with an optional . at the end of string
\d*\.?\d* - 0+ digits, an optional . and again 0+ digits
$ - end of string.
Related
I have this code /^[-0-9\b]+(\.\d{0,2})?$/ for the positive and negative numbers validation
It's working fine for the negative numbers(for ex -12.34), but user can also type a few "-" symbols(ex: -12-3-4.12).
So how can I disable a possibility to type a multiple minus symbols?
You seem to be using the regex for liva validation. In these cases, the regex should comprise only optional parts, those that can match an empty string.
You can use
/^-?\d*\.?\d{0,2}$/
See the regex demo. Details:
^ - start of a string
-? - an optional -
\d* - zero or more digits
\.? - an optional .
\d{0,2} - 0, 1 or 2 digits
$ - end of string.
[-0-9\b]+ Is making it possible to have one or more - characters.
Just change your regex to:
/^-?[0-9\b]+(\.\d{0,2})?$/
Here is the test:
https://regex101.com/r/fbMI7Z/1/
Need some help in designing regular expression to validate hyphen separated floating point numbers in Javascript. So far I have managed to achieve this RegEx:
(^((\\d)+(\.[0-9]+)?)(\-)?((\\d)+(\.[0-9]+)?)$)|^(\\d+)$
It matches the following:
1) 2
2) 2.10
3) 3.10-3.14
The problem with this one is that its also matching "3.103.310" which is wrong number. Much appreciate any help in fixing this issue.
The problem comes from the first alternative that matches 1 or more digits with an optional fractional part ((\d)+(\.[0-9]+)?) and then matches a hyphen and again 1+ digits and again an optional fractional part. Thus, 2 dots are allowed.
You may fix the pattern like this:
^\d+(?:\.\d+)?(?:-\d+(?:\.\d+)?)*$
See the regex demo
Details
^ - start of string
\d+ - 1+ digits
(?:\.\d+)? - an optional non-capturing group:
\. - a dot
\d+ - 1+ digits
(?:-\d+(?:\.\d+)?)* - an non-capturing group matching 0+ occurrences of
- - a hyphen
\d+(?:\.\d+)? - 1+ digits and 1 or 0 occurrences of . and 1+ digits
$ - end of string
Trying to use negative look ahead to match a number if it doesn't precede a % sign.
\d+(?!%) .
8989% .
//matches 898, but not the 9% .
I'd like it to match 8989 as a whole.
Also is it possible use negative look ahead with matching a whole character class or more complex regex?
[\d.+](?!%) .
\d+(\.\d{1,2})?(?!%) .
Which would match decimals not preceding a %
The \d+(?!%) pattern matches one or more digits, and grabs 8989 in 8989% at first, but the (?!%) negative lookahead fails that match, and the engine, seeing the + quantifier, starts backtracking. It discards the last 9 from the match buffer and retries the (?!%) lookahead that succeeds as 898 is not followed with % symbol.
You may use
/\d+(?![\d%])/g
See the regex demo
The (?![\d%]) negative lookahead will fail the match if 1+ digits is followed with any digit or % char, and thus will not return partial matches of 1+ digits that are followed with a % symbol.
I have a problem to check my string for correct time input.
It should be possible to write it like this: (number)d (number)h (number)m
Each part can be left out and the numbers don't have a limit (e.g. 2352h).
Some possibilities are the following:
2424d 23h 15m
523h 526m
235d 252m
829d 926h
I use the following RegEx which works really good but has one problem, which is that one letter can be used multiple times (e.g. 52d 23d).
I want that each letter can be used once. Whitespaces are optional.
This is what I have for now:
^((\d{0,9}[dh]\s?){0,1}(\d{0,9}m)?){1,3}\s*$
Can anyone help me?
Try this Regex:
^(?=.*[hmd]$)\d+(?:d\s*)?\d*(?:h\s*)?\d*(?:m\s*)?$
Click for Demo
Explanation
^ - asserts the start of the string
(?=.*[hmd]$) - positive lookahead to make sure that the string ends with either d, h or m
\d+(?:d\s*)? - matches 1+ occurrences of a digit followed by optional d followed by 0+ spaces
\d*(?:h\s*)? - matches 0+ occurrences of a digit followed by optional h followed by 0+ spaces
\d*(?:m\s*)? - matches 0+ occurrences of a digit followed by optional m followed by 0+ spaces
$ - asserts the end of the string
Another alternative: ^(?=.*[hmd]$)(?:\d+d\s*)?(?:\d+h\s*)?(?:\d+m\s*)?$
I have regular expression which is working fine for decimal .
In below expression I want to remove decimal match to validate Integer
^-?(\d*)(,\d{1,3})*(?:[,]|([.]\d{0,2}))?$
Valid Match : 1,000 & 111, & -1000,00
Invalid Match : -,1 & 1,,,,
Use
^-?(?:\d{1,3}(?:,\d{3})*|\d+)?,?$
|---1---|
|---2-----|
|-3-|
The ^ asserts the position at the start of the string and $ asserts the position at the string end. -? matches an optional -. Part 1 matches 1 to 3 digits, Part 2 matches 0+ sequences of , followed with 3 digits, part 3 is an alternative to Part 1 & 2 combined and matches just 1+ digits. The whole number pattern is optional since the (?:...|...) is quantified with ? - one or zero occurrences.
The ,? before $ allows an optional comma at the end, delete if not required (you mentioned you need to allow 100, in the comments).
See the regex demo