goe
goe

Reputation: 5415

How can I allow a literal dot in a Perl regular expression?

I use this condition to check if the value is alphanumeric values:

$value =~ /^[a-zA-Z0-9]+$/

How can I modify this regex to account for a possible dot . in the value without accepting any other special characters?

Upvotes: 16

Views: 33455

Answers (5)

Eugene Yarmash
Eugene Yarmash

Reputation: 149823

Using the alnum Posix character class, one char shorter :)

value =~ /^[[:alnum:].]+$/; 

Upvotes: 4

brian d foy
brian d foy

Reputation: 132822

If you don't want to allow any characters other than those allowed in the character class, you shouldn't use the $ end of line anchor since that allows a trailing newline. Use the absolute end-of-string anchor \z instead:

 $value =~ /^[a-z0-9.]+\z/i;

Upvotes: 1

FMc
FMc

Reputation: 42421

Don't forget the /i option and the \d character class.

$value =~ /^[a-z\d.]+$/i

Upvotes: 2

Space
Space

Reputation: 7259

Look at perl regular expressions

\w  Match "word" character (alphanumeric plus "_")


$value =~ /^[\w+.]\.*$/;

Upvotes: -1

YOU
YOU

Reputation: 123841

$value =~ /^[a-zA-Z0-9.]+$/

Upvotes: 25

Related Questions