Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one match character OR nothing using regular expression

Tags:

regex

ruby

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?

like image 716
notthehoff Avatar asked Jan 12 '14 20:01

notthehoff


People also ask

How do I not match a character in regex?

You can use negated character classes to exclude certain characters: for example [^abcde] will match anything but a,b,c,d,e characters.

How do you match a single character with a regular expression?

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.

Does empty regex match everything?

An empty regular expression matches everything.

How do I match a character except space in regex?

[^ ] matches anything but a space character.


2 Answers

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 once
  • x{a,b} matches x between a and b times
  • x{a,} matches x at least a times
  • x{,b} matches x up to (a maximum of) b times
  • x* 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}/
like image 95
Dan Tao Avatar answered Oct 10 '22 16:10

Dan Tao


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}
like image 28
user3188140 Avatar answered Oct 10 '22 15:10

user3188140