Require RegEx Engine to fail - regex

I need a regex that matches the following format.
hhhh:mm or hhhhmm
hhhh are hours (first digit should match 1-9 followed by numbers, not more than 4 digits)
mm are minutes (first digit should match 0-5 followed by a number, not more than 2 digits)
The following format should also be possible - mm or m
So far I have
^([1-9]\d{0,3}:?)?([0-5]\d{0,2})?\d
which matches what i want but doesn't fail if I enter e.g. 4444444.
In fact if I have less than 3 digits they should be treated a minutes in the range 0..59.
Any help is appreciated.
I'm not a regex guru at all.

You probably want something like:
^(?:([1-9]\d{0,3}):?)?([0-5]?\d)$
That would allow a single digit minute even if hours part is present. If you don't want that it could be solved in some different ways, eg:
^(?:([1-9]\d{0,3}):?)?([0-5]\d)$|^(\d)$

Related

RegEx for matching digits and one dot with quantifier

I have a specific pattern I'm trying to get. The pattern I'm looking for is the following: 13 digits with a possible dot for a total of min 3 and max 13 digits (including the dot if present) and ending with "/" and number from 1 to 6.
for now I have this pattern
^(\d*|\d*\.?\d*)\/[1-6]$
but this matches 1234/1 or 123456.890123456778/2
but it's not what I need
I tried a few things but I think I missing something
^(\d*|\d*\.?\d*){3-13}\/[1-6]$
Possible match:
1.3/1
123456./2
123456.890123/3
1234567890123/4
123/5
How do I solve this problem?
Your wordings are a little confusing but if I got you correct then you can use this regex,
^(?=.{5,15}$)\d+\.?\d*\/[1-6]$
Explanation:
^ - Start of string
(?=.{5,15}$) - This positive look ahead ensures that the minimum length is 5 and max length is 15 (adding two for last slash and number)
\d+\.?\d* - Starts capturing the text with one or more digits followed by optional dot . and further more zero or more digits
\/[1-6] - Matches a slash and one to six digit
$ - End of string
Regex Demo
Let me know if this works fine for you else list the case for which it doesn't work.

RegEx to check 24 hours time format fails

I have the following RegEx that is supposed to do 24 hours time format validation, which I'm trying out in https://rubular.com
/^[0-23]{2}:[0-59]{2}:[0-59]{2}$/
But the following times fails to match even if they look correct
02:06:00
04:05:00
Why this is so?
In character classes, you're supposed to denote the range of characters allowed (in contrast to the numbers you want to match in your example). For minutes and seconds, this is relatively straight-forward - the following expression
[0-5][0-9]
...will match any numerical string from "00" to "59".
But for the hours, you need to two separate expressions:
[01][0-9]|2[0-3]
...one to match "00" to "19" and one to match "20" to "23". Due to the alternative used (| character), these need to be grouped, which adds another bit of syntax (?:...). Finally we're just adding the anchors ^ and $ for beginning and end of string, which you already had where they belong.
^(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$
You can check this solution out at regex101, if you like.
Your problem is that you understand characters ranges wrong: 0-23 doesn't mean "match any number from 0 to 23", it means: 0-2- match one digit: 0,1 or 2, then match 3.
Try this pattern: (?:[01][0-9]|2[0-3])(?::[0-5][0-9]){2}
Explanation:
(?:...) - non-capturing group
[01][0-9]|2[0-3] - alternation: match whether 0 or one followed by any digits fro 0 to 9 OR 2 followed by 0, 1, 2 or 3 (number from 00-23)
(?::[0-5][0-9]){2} - match : and [0-5][0-9] (basically number from 00-59) twice
Demo
use this (([0-1]\d|[2][0-3])):(([0-5][0-9])):(([0-5][0-9]))
Online demo

Regex check for specific phone phone numbers or extensions

String to be evaluated will be either be a 10 digit number or a 4 digit number.
5551119900 (10 Digit)
9999 (4 Digit)
Need regex to test for specific list of 10 digit numbers or 4 digit numbers. I have the following Regex that almost works
55511199(00|01|02|10|20|30)|(0000|9901|9902|9903|9999)
Above is checking for
5551119900
5551119901
5551119902
5551119910
5551119920
5551119930
0000
9901
9902
9903
9999
ISSUE:
(1) Need match to be exactly 10 digits or 4 digits only.
(2) Pattern match (see link below) is showing an exact match and also a "Group 1". I'm not sure what the group match means or if that is a good thing.
Sample: https://regex101.com/r/BbplFG/1/
Try this version of your regex:
^(?:55511199(?:00|01|02|10|20|30)|(?:0000|9901|9902|9903|9999))$
Demo
I have made several changes here:
Used ?: inside terms in parentheses, to turn off group capturing
Placed the entire pattern inside parentheses
Added starting (^) and ending ($) anchors around the entire pattern

Regex to match less than a two-digit number

I have some records as such, in a file:
A 20 year old programmer
A 52 year old politician
A 12 year old practitioner
Many many more of these...
I want to match only lines that contain a number less than 20. I have tried:
^[0-20]{2}$
But it works for only numbers 0-2. How should I construct a regular expression to match numbers < 20? For instance, it should match:
A 12 year old practitioner
But not
A 20 year old programmer
A 52 year old politician
You may use
\b1?[0-9]\b
See the regex demo
Details
\b - a word boundary
(?:1?[0-9]) - an optional 1 and any digit after it
\b - a word boundary
Word boundary variations
To match anywhere in a string, even if glued to a word:
(?<!\d)1?[0-9](?!\d)
To only match in between whitespaces:
(?<!\S)1?[0-9](?!\S)
Using regex to match digit ranges is usually a bit clumsy, but here, you can do it pretty simply with:
\b1?\d\b
https://regex101.com/r/YCWmNo/2
In plain language: an optional one, followed by a digit. So, any standalone digit is allowed, but a two-digit number needs its first digit to be a 1.
If you want to permit leading zeros, change to \b[01]?\d\b.

number validaiton - javascript regex

I've come up with this regular expression to validate a number which can have Maximum length-13 (including decimal points),Maximum no of decimal points-3,Maximum length of a whole number-12.
^(\d{1,12}([.]\d{1,1})?|\d{1,11}([.]\d{1,2})?|\d{1,10}([.]\d{1,3})?)$
Could anyone tell me if my approach is correct or give me a better solution?
This would also work:
^(?=.{1,13}$)(\d{1,12})(\.\d{1,3})?$
Uses positive look ahead to match the entire string length is ok.
Then it uses a group to match from 1 - 12 digits
Then there's an optional group to match a decimal followed by 1-3 digits.
Edited: Simplified since the rules don't allow a 13 digit integer-part