Ruby-on-rails – Regular expression for only allow numbers and must end with ‘#’ sign

regexrubyruby-on-rails

I want a regular expression that validate a string that allow only numbers and this string must end with a numeral symbol #.

Like:

353645#

Best Answer

Use $ to mean at the end of the string. Try this:

/^[0-9]+#$/

Further explanation:

^      Start of string
[0-9]  Only characters in 0-9
+      At least one
#      Literal #
$      End of string