I need to validate a streetNumber. It has to be a maximum of 4 in length and first 3 should be a number and the last can be a letter( no special character allowed). The letter is not compulsory.
Example 2, 34A, 45, 101 or 121B are all possible numbers. I have this regex but it is not working for me. Can anyone help me.
^[1-9]|[A-Za-z]{1,4}
I expect 2, 34A, 45, 101 or 121B to all pass the regex check
^[1-9]\d{0,2}[A-Za-z]?$
Explanation:
[1-9] a digit between 1 and 9.
\d{0,2} zero or more (up to 2) digits.
[A-Za-z]? a letter (optional)
https://regexr.com/4br78
In case you want to allow 4 digit numbers:
^[1-9]\d{0,2}[A-Za-z\d]?$
Simply use ^[1-9]\d{0,2}[A-Z]?$
Explanation:
^ - beginning of the string
[1-9] - match 1 through 9
\d{0,2} - match zero to two digits
[A-Z]? - match zero or one letter
$ - end of the string
Demo
Related
I am trying to match these use cases in Javascript:
a) 8 Months
b) 18 Years
Numbers could range from 1 to 20 and the strings could only be either "months" or "years"
I am trying this
[0-9]\s(Years|Months)
But it is not working.
You can use this:
([1-9]|1[0-9]|20)\s(Years|Months)
where:
[1-9] matches a digit from 1 to 9
1[0-9] matches a number from 10 to 19
20 matches just 20
Edit: As noticed in a comment, you should use
^([1-9]|1[0-9]|20)\s(Years|Months)$
if the whole string must exactly match with this text.
Another option is prepending the regex with a word boundary (\b) in order to prevent matching cases like "42 Years".
Russian phone number has 11 digits, but i couldn't use a{11}, because phone number can contain brackets and dashes
My regex: ^([\+]?\d){1,2}([\(\-]*[0-9]{3}[\)\-]*)([0-9\-]{7,9})$
Number can have from 1 to 2 characters as a country code
then 3 digits with brackets or not as phone operator code
then from 7-9 digits with dashes or not
For example:
+7(994)849-45-16
+7(994)849-4516
+7(994)84945-16
+7994849-45-16
Question is, how i can count lenght of only digits in regex
You can use
^(?!(?:\D*\d){12})(\+?\d{1,2})([(-]*[0-9]{3}[)-]*)([0-9](?:-?[0-9]){6,8})$
See the regex demo
Details
^ - start of string
(?!(?:\D*\d){12}) - no more than 11 digits are allowed in the string
(\+?\d{1,2}) - Group 1: an optional + and then 1 or 2 digits
([(-]*[0-9]{3}[)-]*) - Group 2: zero or more ( or - chars, three digits, and then again zero or more ) or - chars
([0-9](?:-?[0-9]){6,8}) - Group 3: a digit and then 6 to 8 occurrences of an optional - and a digit
$ - end of string.
This question already has answers here:
Using regular expressions to validate a numeric range
(11 answers)
Closed 2 years ago.
I am trying to write a regular expression which can match enter number string which should be in range of
-50 to 100000000
I have tried expression like
^(-?)(1000|[0-9][0-9][0-9]?)$
but it matches only -1000 to 1000 numbers.
example for to test are simple
-50
-39
9
1000
36900
2000022
Please help me to get such expression which can be match range -50 to 100M
I know we can write simple if condition but I want regex only.
Thanks in Advance.
You can use the following regex:
/^(-50|-[1-4]?\d|100000000|[1-9]?\d{1,7})$/gm
Explanation:
^ match start of string
-50 match -50
OR
- only negative numbers
[1-4]? match 1 to 4 - optional, followed by: any number
OR
100000000 match 100000000
OR
[1-9]? match 1 to 9 - optional followed by:
\d{1,7} match any number 1 to 7 times
$ match end of line
Use global and multiline options.
Regular expression serving leading zeros and positive and negative sign. Works only for integers.
^(-0*(50|[0-4]?[0-9])|\+?0*([0-9]{1,8}|100000000))$
Test case
Using with JavaScript:
let number = -50; /* <-- test number or string here */
let withinTheRange = Boolean(/^(-0*(50|[0-4]?[0-9])|\+?0*([0-9]{1,8}|100000000))$/.exec(number));
I am trying to write a regex for the following condition:
Allow to enter up to 1 decimal place and if I start with integer 0; the next has to be decimal otherwise any other number entered will remove the 0(integer)
The regex that I have provided is /(^$)|(^[0-9]+(\.([0-9]{1})?)?$)/ it accepts 1 decimal place but I am unable to achieve the remaining part.
Edit 1
passed numbers -->
1.3
4.5
0.4
Failed numbers ---->
004
If number starts with 0, next character must be a decimal only
Eg,
0.5 is a pass
005 is a failed
try this:
^([1-9]+\d*.?\d?)|^(0.\d)
If not, see comments and try to better exemplify
Try this regex:
^([0]{1}(\.[0-9]+))$|^([1-9]{1}|[1-9][0-9]+)$
ADJUSTED REGEX:
I have now adjusted the regex as required: all numbers that start with 0 MUST follow a comma, e.g. 0.2 or 0.0002 is accepted. 02 or 00002 is NOT accepted
OK, you want find integer number not start with 0, and find decimal?
Your code is: /(^$)|(^[0-9]+(\.([0-9]{1})?)?$)/
Case 1: integer [1-9]\d*
Case 2: decimal
//match 5.0
patt = \d+\.\d+
//Match 5.1xxxx, 5.2xxx, etc.
patt = \d+\.[1-9]\d*
//match 5.0xxx
patt = \d+\.0\d+
//combine all of them
patt = ([1-9]\d*|\d+\.[1-9]\d*|\d+\.0\d+)
//Final
patt = /(^$)|(([1-9]\d*|\d+\.[1-9]\d*|\d+\.0\d+)?$)/
I will explain some pattern above:
\d == [0-9] //match 1 character between 0 and 9
\d* == \d{0,} //match 0 or more character
\d+ == \d{1,} //match at least one character
I have tried Regex accept numeric only. First character can't be 0 and What is the regex for "Any positive integer, excluding 0" however that didn't work as per my requirements.
I want exact 6 digit numeric number but shouldn't start with 0
I've tried
^[1-9][0-9]{6}*$
^([^0][0-9]){6}$
...
Need fine tuning.
The problem with ^[1-9][0-9]{6}*$ is it is an invalid regex because of {6}* and ^([^0][0-9]){6}$ is that it is allowing any character that is not 0 followed by six digits.
Use
^[1-9][0-9]{5}$
Explanation:
^: Starts with anchor
[1-9]: Matches exactly one digit from 1 to 9
[0-9]{5}: Matches exactly five digits in the inclusive range 0-9
$: Ends with anchor
Regex101 Playground
HTML5 Demo:
input:invalid {
color: red;
}
<input type="text" pattern="[1-9][0-9]{5}" />
This regular expression covers;
PIN code doesn't start from zero
Allows 6 digits only
Allows 6 consecutive digits (e.g. 431602)
Allows 1 space after 3 digits (e.g. 431 602)
([1-9]{1}[0-9]{5}|[1-9]{1}[0-9]{3}\\s[0-9]{3})
Some websites and banks have the habit of spacing pincode after 3 digits.
To match both 515411 and 515 411 the following pattern will help.
^[1-9]{1}[0-9]{2}\s{0,1}[0-9]{3}$
^[1-9]{1} - PIN Code that starts with digits 1-9
[0-9]{2} - Next two digits may range from 0-9
\s{0,1} - Space that can occur once or never
[0-9]{3}$ - Last 3 needs to be digits ranging from 0-9