This question already has answers here:
Regex to detect one of several strings
(7 answers)
Closed 2 years ago.
I am learning more and more about regex and while reading about it I was wondering if regex can do something like this. Looking in a string for, for example numbers that are 3 digits long, where the second digit is 2 higher than the first digit, and the third digit is 4 higher than the second digit.
For example:
Matches:
137,
248,
359
Would regex be capable of such thing? From my limited knowdlege it does not seem to be possible.
Regex is only capable of matching string, not performing mathematcial logic on numbers, so it is not able to determine that two digits differ by certain quantity.
So answer is no.
BUT, that rule limits 3-digit numbers to a few, so you can write regex alternation to do that:
(?:137|248|359)
Explanation:
(?:...) - non-cpaturing group
| - alternation
Related
This question already has answers here:
jQuery current regular expression
(2 answers)
Closed 1 year ago.
I'm trying to only allow the following numbers in my input:
if the input starts with 0-9 and a decimal follows then max length would be 4
ex. 0.99 or 1.23
if the input starts with 10-99 and a decimal follows then the max length would be 5
ex. 10.12 or 99.99
so far I have this and it works but not as strictly as I'd like
/^[0-9]+(\.[0-9]*)?$/
I believe this is what you are looking for. The {1,2} thing says it can have one or two of the instance before it. The \d specifies a digit.
/^\d{1,2}\.\d{1,2}$/
If you require two decimal places always:
/^\d{1,2}\.\d{2}$/
Try this:
console.log(/^[0-9][0-9]?(\.[0-9]{0,2})$/.test("0.98"));
console.log(/^[0-9][0-9]?(\.[0-9]{0,2})$/.test("0.987"));
console.log(/^[0-9][0-9]?(\.[0-9]{0,2})$/.test("00.98"));
console.log(/^[0-9][0-9]?(\.[0-9]{0,2})$/.test("00.987"));
This question already has an answer here:
Restricting character length in a regular expression
(1 answer)
Closed 3 years ago.
Im trying to add a length requirement to the below code.
Code: ^[a-zA-z0-9\!\$\%\&]+(?: [a-zA-z0-9\!\$\%\&]+)*$
I want a sentence with spaces to only have around 1 to 10 characters. I want to count spaces as well. The code provided doesn't allow leading or trailing space but space between.
A sentence limited to 10 chars is obscenely small but you can use:
^(?=^.{1,10}$)[a-zA-z0-9\!\$\%\&]+(?: [a-zA-z0-9\!\$\%\&]+)*$
(?=^.{1,10}$) = ensure that between 1 and 10 chars exist between the start and end of the string.
https://regex101.com/r/zsa1N9/1
I see your new comment about Javascript. You would be better off adding a check for .length in addition to your regex.
This question already has answers here:
Regular expression for GB based and only numeric phone number
(5 answers)
Closed 4 years ago.
So, after some user confusion, I need to tweak a regular expression for all phone number variations you could think of.
I currently have the following regular expression:
^[+]?[-\s.]?[(]?[0-9]{3}[)]?[-\s.]?[0-9]{3}[-\s.]?[0-9]{4,6}$
It matches fairly well, but I'd like to be able to match this to the following variations:
+44 01726 400480
+44 1726 400480
+44 01726 400 480
+44 1726 400 480
01726 400480
01726 400 480
N.B. I understand that similar questions to this have been asked previously, but I feel that adding the above variations would be perfect for the community.
You can use
^\+?(?:[- .]?\d{2})?[- .]?\d{4,5}[- .]?\d{3}[- .]?\d{3}$
https://regex101.com/r/DiJLVb/2
Note that [0-9] simplifies to \d, and that character sets with only a single character in them simplify to just that character. Also note that using a literal space is a bit more reliable than \s if you only want to match a literal space - otherwise, other whitespace characters like newlines will be matched too, which may well not be desirable.
Details:
\+? - Optional plus
(?:[- .]?\d{2})? - Optional leading two digits
[- .]?\d{4,5} - Four or five digits
[- .]?\d{3}[- .]?\d{3} - Six digits, possibly separated by a character
you can try the following https://www.regexpal.com/?fam=105302
^[+]?[-\s.]?[(]?[0-9]{2,3}?[)]?[-\s.]?([0-9]{3,4})?[-\s.]?[0-9]{3,6}[-\s.]?[0-9]{3,6}$
This question already has answers here:
RegExp range of number (1 to 36)
(8 answers)
Closed 6 years ago.
I'm looking for a way to validate a phone number with the length of 7, and within the rage of 8600000–9999999 (This is a region in New Zealand, witch is why google was no help as no one talks about New Zealand -- ever). Does anyone know a regEx that can do this?
I'm new to JS, but I think that a Regular Expression would be the best way to do this. (If not let me know.)
Note: The input data type of the phone number is text. (I know I can use number but I ran into bugs with it.)
EDIT -- no lines / separation, just numbers. (Also very fast answers just wow)
Thanks in advance !
This one's actually pretty easy:
/8[6-9][0-9]{5}|9[0-9]{6}/
As long as you don't need separators, anyways.
It's also pretty simple to follow:
8 matches the literal character 8.
[6-9] matches any single character that is in the ASCII range between the characters 6 and 9 (inclusive). This means that it will match any single 6, 7, 8, or 9.
Likewise, [0-9] matches any single character that is in the ASCII range betwen the characters 0 and 9. This is synonymous with "any single digit".
{5} means "match the preceding token 5 times". In this case, it's applied to [0-9], meaning it'll match 5 digits in a row.
The | (pipe) character in a regex is an alternation - it means "match either the pattern on the right or the left". This is how the regex handles the two different cases - 8600000-8999999 is handled by the pattern on the left, while 9000000-9999999 is handled by the pattern on the right.
You can use the regular expression /(?:9[0-9]|8[6-9])[0-9]{5}/.
You can click to play with it on regex101.com, and see some test cases.
Here's the breakdown (provided by regex101.com):
(?:9[0-9]|8[6-9]) Non-capturing group
1st Alternative: 9[0-9], left of |
9 matches the character 9 literally
[0-9] match a single character present in the list below
0-9 a single character in the range between 0 and 9
2nd Alternative: 8[6-9], right of |
8 matches the character 8 literally
[6-9] match a single character present in the list below
6-9 a single character in the range between 6 and 9
[0-9]{5} match a single character present in the list below
0-9 a single character in the range between 0 and 9
{5} exactly 5 times
This question already has answers here:
Regex that matches numeric with up to 2 decimal places
(8 answers)
Closed 9 years ago.
I am new to regex and need to write a regex to match both "100" and "100.00" in an amount value. The last two digits are matched only if these are following a "."
I have tried : ^\d+?(?=([.]{1})\d{2})$ - without any luck
Help is much appreciated
You don't need a lookahead here. You can use an optional non-capture group
^\d+(?:\.\d{2})?$
Use this regex: /^\d+(?:\.\d{2})?$/
Variation on previous answers
^\d{1,8}([\.,]\d{1,2})?$
I've found that users will still enter money values as 1.5 when they mean 1.50, so we use a regex to allow for that as well.
The number of decimal places can be adjusted, as can the number of digits before the decimal point