Regular expression for hyphen separated floating point numbers - javascript

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

Related

Regex to validate optional parts of a bigger format

The general format is:
YYYY/MM/DD/INFO
Only the separators / are mandatory.
Each part is optional.
YYYY - exactly 4 numbers.
MM - exactly 2 numbers.
DD - exactly 2 numbers.
INFO - any sequence of letters, spaces or hyphens.
So these are valid strings:
2020/06/25/XYZConf
2020///XYZConf
2020//25/XYZConf
2020/06//XYZConf
//25/XYZConf
///
I'm really struggling to come up with a regex that validates optional parts while maintaining the integrity of the string as a whole.
How would you write this regular expression?
PS: This needs to be a regular expression as it will be part of a third-party lexer that doesn't accept anything else.
Relevant posts:
Regex to validate comma-separated numbers with optional fractional parts
Regex for dates that matches for every stage of valid date entry
You could try something like:
^(?:\d{4})?\/(?:(?:\d\d)?\/){2}(?:[A-Za-z\s-]+)?$
See the Online Demo
I believe that you are looking for optional (non)capturing groups. The pattern above matches:
^ - Start string ancor.
(?: - Open 1st non-capturing group.
\d{4} - Match 4 digits.
)? - Close 1st non-capturing group and make it optional.
\/ - Match a forward slash.
(?: - Open 2nd non-capturing group.
(?: - Open 3rd non-capturing group.
\d\d - Match two digits.
)? - Close 3rd non-capturing group and make it optional.
\/ - Match a forward slash.
){2} - Close 2nd non-capturing group and make it match twice.
(?: - Open 4th non-capturing group.
[A-Za-z\s-]+ - Match upper- and lowercase letters, a space and hyphen at least one time (in any sequence as per your OP).
)? - Close 4th non-capturing group and make it optional.
$ - End string ancor.

Making dot optional with limited characters

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.

Regex for d h m time string

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*)?$

Generic JavaScript regex validating a positive number with or without commas as thousand separators and an optional fractional part

I have following regex to validate numbers in input
var reg = /^\d+$/;
Now i want to allow ,(commas) and .(period) in number field as following will some one help me writing regex to allow following number format ?
10000000
10,000,000
10000000.00
You may use
/^(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d+)?$/
See the regex demo
If you only need to allow 2 digits after the decimal separator, replace (?:\.\d+)? with (?:\.\d{1,2})?.
Details:
^ - start of string
(?:\d{1,3}(?:,\d{3})*|\d+) - 2 alternatives:
\d{1,3}(?:,\d{3})+ - 1 to 3 digits and one or more sequences of a comma and 3 digits
\d+ - 1 or more digits
(?:\.\d+)? - an optional sequence of:
\. - a dot
\d+ - 1 or more digits
$
You could use
^((\d{1,2}(,\d{3})+)|(\d+)(\.\d{2})?)$
see Regex101
or
^((\d{1,2}(,\d{3})+)|(\d+))(\.\d{2})?$
if you want 10,000,000.00 to get matched to.

Regular expression for thousand separator without decimal point

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

Categories

Resources