I am trying to take a block of numbers that may, or may not, have dividers and return them in a standard format. Using SSN as an example:
ex1="An example 123-45-6789"
ex2="123.45.6789 some more things"
ex3="123456789 thank you Ruby may I have another"
should all go into a method that returns "123-45-6789" Basically, anything(INCLUDING nothing) except a number or letter should return a SSN in a XXX-XX-XXXX format. The part that is stumping is a way to regular expressions to identify that there can be nothing.
What I have so far in IDENTIFYING my ssn:
def format_ssns(string)
string.scan(/\d{3}[^0-9a-zA-Z]{1}\d{2}[^0-9a-zA-Z]{1}\d{4}/).to_a
end
It seems to work for everything I expect EXCEPT when there is nothing. "123456789" does not work. Can I use regular expressions in this case to identify lack of anything?
You can use negated character classes to exclude certain characters: for example [^abcde] will match anything but a,b,c,d,e characters.
Match any specific character in a setUse square brackets [] to match any characters in a set. Use \w to match any single alphanumeric character: 0-9 , a-z , A-Z , and _ (underscore). Use \d to match any single digit. Use \s to match any single whitespace character.
An empty regular expression matches everything.
[^ ] matches anything but a space character.
This has already been shared in a comment, but just to provide a complete-ish answer...
You have these tools at your disposal:
x
matches x
exactly oncex{a,b}
matches x
between a
and b
timesx{a,}
matches x
at least a
timesx{,b}
matches x
up to (a maximum of) b
timesx*
matches x
zero or more times (same as x{0,}
)x+
matches x
one or more times (same as x{1,}
)x?
matches x
zero or one time (same as x{0,1}
)So you want to use that last one, since it's exactly what you're looking for (zero or one time).
/\d{3}[^0-9a-zA-Z]?\d{2}[^0-9a-zA-Z]?\d{4}/
Have you tried to match 0 or 1 characters between your numbers?
\d{3}[^0-9a-zA-Z]{0,1}\d{2}[^0-9a-zA-Z]{0,1}\d{4}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With