Kashiftufail
Kashiftufail

Reputation: 10885

Regular expression for only allow numbers and must end with single '#' sign at end

Hi i want to validate a string with in which only numbers are allowed and only one # sign at

the end.I use this but it allow double # sign at end.

    /^[0-9]+#/

How i can refine it to only allow single # sign at end of string like 1345#

Upvotes: 1

Views: 1823

Answers (2)

jdoe
jdoe

Reputation: 15771

Don't use ^ and $. Use \A and \z instead! It's a big gotcha!

/\A[0-9]+#\z/

^ and $ are used to specify the end of the LINE, not sting!

# don't do this!!!
/^[0-9]+\#$/ =~ "12#\nfoo" # MATCHES!!!

I hope it'll help someone else!

Upvotes: 5

Aprillion
Aprillion

Reputation: 22324

use $ for matching the end of string

/^[0-9]+#$/

Upvotes: 2

Related Questions