Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

perl

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?

like image 994
goe Avatar asked Apr 15 '10 11:04

goe


People also ask

How do you add a dot in regex?

The dot in regular expressionsThe dot essentially matches any character. For example, the following expression means "a digit plus any other character": [0-9]. Really, this expression means "any number (including zero) of any character, followed by twn digits, followed by any number of any character".

Is dot a special character in regex?

In regular expressions, the dot or period is one of the most commonly used metacharacters. Unfortunately, it is also the most commonly misused metacharacter. The dot matches a single character, without caring what that character is. The only exception are line break characters.

What is * in Perl regex?

Regular Expression (Regex or Regexp or RE) in Perl is a special text string for describing a search pattern within a given text. Regex in Perl is linked to the host language and is not the same as in PHP, Python, etc. Sometimes it is termed as “Perl 5 Compatible Regular Expressions“.

How do you escape a dot in regex?

(dot) metacharacter, and can match any single character (letter, digit, whitespace, everything). You may notice that this actually overrides the matching of the period character, so in order to specifically match a period, you need to escape the dot by using a slash \.


1 Answers

$value =~ /^[a-zA-Z0-9.]+$/
like image 58
YOU Avatar answered Sep 20 '22 04:09

YOU