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*)?$
Related
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.
Use lookaheads to match a string that is greater than 5 characters long and have two consecutive digits.
I know the solution should be
/(?=\w{6,})(?=\D*\d{2})/
But why the second element is
(?=\D*\d{2})
Instead of
(?=\d{2})
Please help me to understand this.
Actually, /(?=\w{6,})(?=\D*\d{2})/ does not ensure there will be a match in a string with 2 consecutive digits.
Check this demo:
var reg = /(?=\w{6,})(?=\D*\d{2})/;
console.log(reg.test("Matches are found 12."))
console.log(reg.test("Matches are not found 1 here 12."))
This happens because \D* only matches any non-digit chars, and once the \w{6,} matches, (?=\D*\d{2}) wants to find the two digits after any 0+ digits, but it is not the case in the string.
So, (?=\w{6,})(?=\D*\d{2}) matches a location in the string that is immediately followed with 6 or more word chars and any 0+ non-digit chars followed with 2 digits.
The correct regex to validate if a string contains 6 or more word chars and two consecutive digits anywhere in the string is
var reg = /^(?=.*\w{6,})(?=.*\d{2})/;
Or, to support multiline strings:
var reg = /^(?=[^]*\w{6,})(?=[^]*\d{2})/;
where [^] matches any char. Also, [^] can be replaced with [\s\S] / [\d\D] or [\w\W].
And to match a string that is greater than 5 characters long and have two consecutive digits you may use
var reg = /^(?=.*\d{2}).{5,}$/
var reg = /^(?=[\s\S]*\d{2})[\s\S]{5,}$/
where
^ - start of string
(?=[\s\S]*\d{2}) - there must be two digits anywhere after 0+ chars to the right of the current location
[\s\S]{5,} - five or more chars
$ - end of string.
The lookahead has to allow the 2 digits anywhere in the input. If you used just (?=\d{2}) then the 2 digits would have to be at the beginning.
You could also use (?=.*\d{2}). The point is that \d{2} has to be preceded by something that can match the rest of the input before the digits.
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
I have two patterns for javascript:
/^[A-z0-9]{10}$/ - string of exactly length of 10 of alphanumeric symbols.
and
/^\d+$/ - any number of at least length of one.
How to make the expression of OR string of 10 or any number?
var pattern = /^([A-z0-9]{10})|(\d+)$/;
doesn't work by some reason. It passes at lest
pattern.test("123kjhkjhkj33f"); // true
which is not number and not of length of 10 for A-z0-9 string.
Note that your ^([A-z0-9]{10})|(\d+)$ pattern matches 10 chars from the A-z0-9 ranges at the start of the string (the ^ only modifies the ([A-z0-9]{10}) part (the first alternative branch), or (|) 1 or more digits at the end of the stirng with (\d+)$ (the $ only modifies the (\d+) branch pattern.
Also note that the A-z is a typo, [A-z] does not only match ASCII letters.
You need to fix it as follows:
var pattern = /^(?:[A-Za-z0-9]{10}|\d+)$/;
or with the i modifier:
var pattern = /^(?:[a-z0-9]{10}|\d+)$/i;
See the regex demo.
Note that grouping is important here: the (?:...|...) makes the anchors apply to each of them appropriately.
Details
^ - start of string
(?: - a non-capturing alternation group:
[A-Za-z0-9]{10} - 10 alphanumeric chars
| - or
\d+ - 1 or more digits
) - end of the grouping construct
$ - end of string
I have the following Regular expression :
(?i:(?:(?:(?:fbx|fo))\d+)|(?:(09|0[1-5])\s?(?:\d{2}\s?){4})(?:(#freeadsl)?))
I tested the expression in https://regex101.com/ and it works.
But in javascript, it dosen't work.
After doing a search, it turned out that the problem is that javascript doesn't accept regex ignore case ?i.
What's the best solution to remedy this problem.
Any help, i'll appreciate it, thanks !
JavaScript regex engine does not support inline modifier groups. You may use a i modifier in the JS regex and remove unnecessary non-capturing groups to reduce your regex to
var rx = /(?:fbx|fo)\d+|(?:09|0[1-5])\s?(?:\d{2}\s?){4}(?:#freeadsl)?/i;
^
See the regex demo. The /i at the end makes the letters in the pattern match both lower- and uppercase letters.
Details:
(?:fbx|fo)\d+ - fbx or fo substring followed with 1+ digits
| - or
(?:09|0[1-5]) - 09 substring or 0 followed with 1 to 5 digit.
\s? - an optional (1 or 0) whitespaces
(?:\d{2}\s?){4} - 4 occurrences of:
\d{2} - 2 digits
\s? - an optional (1 or 0) whitespaces
(?:#freeadsl)? - an optional #freeadsl substring.