REGEX for phone number - Ruby - ruby

I want to make a REGEX for a phone number validation that only allows:
7 digits (5557865) that's formatted exactly like the example.
I'm pretty unfamiliar with regex or else I would tackle this myself. Hopefully that is enough info let me know if you need anything else.

Working example https://regex101.com/r/oZ2yQ0/1
\(\d{7}\)
\( matches the character ( literally
\d{7} match a digit [0-9]
Quantifier: {7} Exactly 7 times
\) matches the character ) literally

If it is just to match 7 digit number, you can use \d{7} or [0-9]{7}.

can be just with
/\d{7}/ #or
/\d{1..7}/ #or
/\d[0-9]{7}/
the \d matches digits and {7} the number of the digits.

There's a few ways to tackle this:
# Ensure the number consists entirely of seven digits, nothing else.
number.match(/\A\d{7}\z/)
# Remove all non-digit characters (\D) and test that the length is 7.
number.gsub(/\D/, '').length == 7
# Test that this is either NNN-NNNN or NNNNNNN.
number.match(/\A\d\d\d\-\d\d\d\d\z/)
Normally you want to make your validation methods as lenient as possible while still ensuring things are valid.

Related

Regex not matching number as expected

I have the following string that I want to extract from:
/Monovolume/Honda+HR+V+1+6-11399031.htm
What I want to extract is the 8 digit number at the end which I tried with the following regex:
Monovolume\/.+(\d{7,})
It says 7 or more because there are cases where there are only 7 digits. The match, however, is only 7 digits and not 8 as in the above string. When I run the part in parentheses only I get the right result. What is causing this behaviour and how can I fix it?
P.S. I can't put the "-" in the regex, because its appearance is coincidental.
You're very close. Your problem is that your .+ will always consume one of the digits, as regex is by default "greedy".
I'm not sure about your requirements, but you could do a lazy match:
Monovolume\/.+?(\d{7,})
/|\
|
It will essentially repeat as few times as possible (when it reaches 7 or more digits).
See it live
More info here: Regex Lazy Quantification

phone regex does not completely working

In my country the phone numbers follow a format like this (XX)XXXX-XXXX. But enter phone numbers according to the pattern in input texts it's too mainstream. Some people follow, but some people don't. I'd like to make a regex to catch all possible cases. By now it look like this:
/^[\(]?\d{2}?[\)]?\d{4}[. -]?\d{4}$/
And I prepared some test cases to prove the regex's functionality
# GOOD PHONES #
8432115262
843211 5262
843211.5262
843211-5262
32115262
3211.5262
3211 5262
3211-5262
(84)32115262
(84)3211.5262
(84)3211 5262
(84)3211-5262
# BAD PHONES #
!##$%*()
()32115262
()1231 3213
()1231.3213
()1231-3213
().3213
()-3213
()3213.
()3213-
3211-5a62
sakdiihbnmwlzi
Unfortunately, the wrong case ()32115262 is bypassing the regex. Altought it is clear why. this part [\(]?\d{2}?[\)]? is responsable for the mistake. From left to right, you can enter zero or one of (; You can enter zero or two digits; You can enter zero or one of ).
I'd like that part should be like this: If you put (, you will have to enter two digits and ), else you can enter zero or two digits. Something like this or with simmilar semantics is possible in regex world?
Thanks in advance
Something like this perhaps:
/^(?:\(\d{2}\)|\d{2}?)\d{4}[. -]?\d{4}$/
I used a non-matching group (?: ... ) and alternation to provide two possible options for the first part of the phone number.
Either it is \(\d{2}\) which means brackets with exactly two digits, or it is \d{2}? which means two digits or empty string.
Combine these two options together with | (which means OR) and you get the first part of the regex above: (?:\(\d{2}\)|\d{2}?)
It seemed to work for all your test cases!
try with this: ^(?:\(\d\d\)|\d\d)?\d{4}[. -]?\d{4}$
If pattern matches (..) then have to match 2 digits inside.

How to find whole complete number with ruby regex

I'm looking to find the first whole occurance of a number within a string. I'm not looking for the first digit, rather the whole first number. So, for example, the first number in: w134fklj342 is 134, while the first number in 1235alkj9342klja9034 is 1235.
I have attempted to use \d but I'm unsure how to expand that to include multiple digits (without specifying how long the number is).
I think, you're looking for this regex
\d+
"Plus" means "one or more". This regex will match all numbers within a string, so pick first one.
strings = ['w134fklj342', '1235alkj9342klja9034']
strings.each do |s|
puts s[/\d+/]
end
# >> 134
# >> 1235
Demo: http://rubular.com/r/YE8kPE2SyW
The easiest way to understand regexes is to think of eachbit is one character; e.g: \d or [1234567890] or [0-9] will match one digit.
To expand this one character you have 2 basic options: * and +
* will match the character 0 or more times
+ will match it one or more times
Like Sergio said you should use \d+ to match many digits.
Excellent tutorial for regexes in general: http://www.regular-expressions.info/tutorial.html

regex for matching german postal codes but not a

following string:
23434 5465434
58495 / 46949345
58495 - 46949345
58495 / 55643
d 44444 ssdfsdf
64784
45643 dfgh
58495/55643
48593/48309596
675643235
34565435 34545
it only want to extract the bold ones. its a five digit number(german).
it should not match telephone numbers 43564 366334 or 45433 / 45663,etc as in my example above.
i tried something like ^\b\d{5} but thats not a good beginning.
some hints for me to get this working?
thanks for all hints
You could add a negative look-ahead assertion to avoid the matches with phone numbers.
\b[0124678][0-9]{4}\b(?!\s?[ \/-]\s?[0-9]+)
If you're using Ruby 1.9, you can add a negative look-behind assertion as well.
You haven't specified what distinguishes the number you're trying to search for.
Based on the example string you gave, it looks like you just want:
^(\d{5})\n
Which matches lines that start with 5 digits and contain nothing else.
You might want to permit some spaces after the first 5 digits (but nothing else):
^(\d{5})\s*\n
I'm not completely sure about the specified rules. But if you want lines that start with 5 digits and do not contain additional digits, this may work:
^(\d{5})[^\d]*$
If leading white space is okay, then:
^\s*(\d{5})[^\d]*$
Here is the Rubular link that shows the result.
^\D*(\d{5})(\s(\D)*$|()$)
This should (it's untested) match:
line starting with five digits (or some non-digits and then five digits), then
a space, and ending with some non-numbers
line starting and ending with five
digits (or some non-digits and then five digits)
\1 would be the five digits
\2 would be the whole second half, if any
\3 would be the word after the digits, if any
edited to fit the asker's edited question
edit again: I came up with a much more elegant solution:
^\D*(\d{5})\D*$

Regular expression to exclude special characters

I need a regex for a password which meets following constraints in my rails project:
have a minimum of 8 and a maximum of 16 characters
be alphanumeric only
contain at least one letter and one number.
My current regex is:
/^(?=.*\d)(?=.*([a-z]|[A-Z])).{8,16}$/
This allows me all the restrictions but the special characters part is not working. What is it that I am doing wrong. Can someone please correct this regex?
Thanks in advance.
/^(?=.*\d)(?=.*[a-zA-Z])[0-9a-zA-Z]{8,16}$/
The last part of your regex, .{8,16}, allows any character with a dot.
The lookahead only makes sure that there's at least one digit and one letter - it doesn't say anything about other characters. Also, note that I've updated your letter matching part - you don't need two character classes.
Disallowing special characters in a password is totally counter intuitive. Why are you doing that?

Resources