Regex - accept value between 5 and 555 - regex

I have a text box. This text box should accept only values between 5 and 555. But I am unable to achieve it.
I have tried the below:
/^[0-9]{5, 500}$/
Kindly help me in this case.

What you need:
^([5-9]|\d\d|[1-4]\d\d|5[0-4]\d|55[0-5])$
It uses | to have 3 different statements to deal with 1-digit, 2-digit and 3-digit numbers.
Live Demo on Regex101
How it works:
^ # String starts with ...
(
[5-9] # 1-Digit - Any digit 5 or over (5 to 9)
| # OR
\d\d # 2-Digit - Any 2 digits (since any 2-digit number will be within your range)
| # OR
[1-4]\d\d # 3-Digit (below 500) - Any digit 1 to 5 (100 to 500), followed by any 2 digits
| # OR
5[0-4]\d # 3-Digit (above 500, below 550) - 5, followed by any digit 0 to 4 (500 to 540),
# followed by any digit
| # OR
55[0-5] # 3-Digits (550 or above) - 55, followed by any digit 0 to 5 (550 to 555)
)
$ # ... String ends with

This should work (though not recommended)
^(55[0-5]|5[0-4][0-9]|[1-4][0-9][0-9]|[1-9][0-9]|[5-9])$
Regex Demo
Proof of correctness

Related

regex input validation for mobile number 03025498448 using C#

I am trying to do regex validation for 11 digit mobile number of type 03025398448.Where first 3 digits are constant 030 and remaining 8 digits are from 0 to 9 (any number) and 1st digit could be written in +92 format .So, help me for this number regex code
If the number should start with 030 and +92 is optional and when using +92 you should omit the leading zero, you could use:
^(?:\+9230|030)?\d{8}$
Explanation
^ # From the beginning of the string
(?: # Non capturing group
\+9230|030 # Match +9230 or 030
)? # close capturing group and make it optional
\d{8} # Match 8 digits
$ # The end of the string
In C# you could use this as string pattern = #"^(?:\+9230|030)?\d{8}$";
C# code
You can use this regular expression:
^((\+?92)30[0-9]{8}|030[0-9]{8})$
Explanation
BeginOfLine
CapturingGroup
GroupNumber:1
OR: match either of the followings
Sequence: match all of the followings in order
CapturingGroup
GroupNumber:2
Sequence: match all of the followings in order
Repeat
+
optional
9 2
3 0
Repeat
AnyCharIn[ 0 to 9]
8 times
Sequence: match all of the followings in order
0 3 0
Repeat
AnyCharIn[ 0 to 9]
8 times
EndOfLine

Extract number from string with regex in groovy

I have the following type of strings with numbers within:
(12 - 17)
(4.5 - 5.5)
(4 - 10)
My code which works for the first two examples is like this:
def numbers=range=~/\d{1,3}.?\d{1,2}?/
where the result for numbers is :
[12,17]
[4.5,5.5]
but for the last is only
[10] it does not get the 4.
Does anyone see where I am wrong?
Your regex requires at least 2 integer digits on end. Look: \d{1,3} matches 1 to 3 digits, .? matches any character but a newline 1 or 0 times (optional) and \d{1,2}? matches 1 or 2 digits (the {1,2}? is a lazy version of a limiting quantifier meaning it will match as few digits as possible to return a valid match).
Use
/\d{1,3}(?:\.\d{1,2})?/
See the regex demo.
Explanation:
\d{1,3} - 1 to 3 digits
(?:\.\d{1,2})? - 1 or 0 sequences (due to ?) of:
\. - a literal period
\d{1,2} - 2 or 1 digits (this is a greedy version of the limiting quantifier).
Here is a Groovy demo:
def x = "(12 - 17)(4.5 - 5.5)(4 - 10)"
def res = x.findAll(/\d{1,3}(?:\.\d{1,2})?/)
println res
Output: [12, 17, 4.5, 5.5, 4, 10]

Regex: find a variable number sequence

I am looking for a specific sequence of numbers in a line. I can best explain with an example:
00001 # first search criteria - line 1
00010 # second search criteria - line 2
So every line has 5 digits of either 0 or 1. I am looking for the combination of all 0 except for 1 digit that can be a 1. This 1 can be in any position of the 5 digits.
The regex code I have for 5 digits of 0 is
^((0\s*?){5}) # there may be spaces between the numbers
The line 1 case above would be selected with following regex code:
^((0\s*?){4})\s*(1)
My question is how I could write in regex code the changing position of 1 to cover the 5 cases/positions.
Thank you.
You can use a lookahead based regex for this:
^(?=[0\s]*1[0\s]*$)(?:\s*[01]\s*){5}$
RegEx Demo
Lookahead (?=[0\s]*1[0\s]*$) will enforce only single 1 at any position in input where as (?:\s*[01]\s*){5} will make sure that input has only 0 and 1 with 5 digits length also allowing white-spaces anywhere.
You can use two conditionals.
First one insures 1 is not found again.
Second one insures 1 is found.
(?:((?(1)(?!))1)|0){5}(?(1)|(?!))
Expanded
(?:
( # (1 start)
(?(1) (?!) ) 1
) # (1 end)
| 0
){5}
(?(1) | (?!) )

Regex pattern for validation where 2 sections vary in length interdependently

I'm trying to workout a Regex pattern for validating a string that consists of 2 parts that vary in length but the overall length remains the same.
Overall length = 7
start section alpha characters only 1-3 characters
end section 4-6 digits
combinations 1 Alpha + 6 digits or 2 Alpha + 5 digits or 3 Alpha + 4 digits.
In the second and third option the first character is allowed to be a space.
What I have so far is ^(?:([\sA-Z][A-Z]{2})(\d{4})|[\sA-Z]A-Z|A-Z)$
Can that be simplified?
How can I have and optional Alpha character at the end?
You need a look ahead to assert the overall length and a negative look ahead to prevent the "space digit" start:
^(?=.{7}$)(?! \d) ?[a-zA-Z]{1,3}\d{4,6}$
See a live demo with several edge cases.
This might work
# (?i)^(?=.{7}$)(?:[a-z]{1,3}|[ ][a-z]{2,3})\d{4,6}[a-z]?$
(?i) # Case independent
^ # BOL
(?= .{7} $ ) # 7 chars total
(?:
[a-z]{1,3} # 1 to 3 alpha
|
[ ] [a-z]{2,3} # or, space plus 2 to 3 alpha
)
\d{4,6} # 4 to 6 digits
[a-z]? # optional alpha char
$ # EOL

Regular expression for phone numbers

I'm trying:
\d{3}|\d{11}|\d{11}-\d{1}
to match three-digit numbers, eleven-digit numbers, eleven-digit followed by a hyphen, followed by one digit.
But, it only matches three digit numbers!
I also tried \d{3}|\d{11}|\d{11}-\d{1} but doesn't work.
Any ideas?
There are many ways of punctuating phone numbers. Why don't you remove everything but the digits and check the length?
Note that there are several ways of indicating "extension":
+1 212 555 1212 ext.35
If the first part of an alternation matches, then the regex engine doesn't even try the second part.
Presuming you want to match only three-digit, 11 digit, or 11 digit hyphen 1 digit numbers, then you can use lookarounds to ensure that the preceding and following characters aren't digits.
(?<!\d)(\d{3}|\d{11}|\d{11}-\d{1})(?!\d)
\d{7}+\d{4} will select an eleven digit number. I could not get \d{11} to actually work.
This should work: /(?:^|(?<=\D))(\d{3}|\d{11}|\d{11}-\d{1})(?:$|(?=\D))/
or combined /(?:^|(?<!\d))(\d{3}|\d{11}(?:-\d{1})?)(?:$|(?![\d-]))/
expanded:
/ (?:^ | (?<!\d)) # either start of string or not a digit before us
( # capture grp 1
\d{3} # a 3 digit number
| # or
\d{11} # a 11 digit number
(?:-\d{1})? # optional '-' pluss 1 digit number
) # end capture grp 1
(?:$ | (?![\d-])) # either end of string or not a digit nor '-' after us
/