Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx for matching "A-Z, a-z, 0-9, _" and "."

Tags:

regex

I need a regex which will allow only A-Z, a-z, 0-9, the _ character, and dot (.) in the input.

I tried:

[A-Za-z0-9_.]  

But, it did not work. How can I fix it?

like image 537
Alon Gubkin Avatar asked Nov 12 '09 11:11

Alon Gubkin


People also ask

What is the regular expression for identifiers with AZ and 0-9 }?

Most characters, including all letters ( a-z and A-Z ) and digits ( 0-9 ), match itself. For example, the regex x matches substring "x" ; z matches "z" ; and 9 matches "9" . Non-alphanumeric characters without special meaning in regex also matches itself. For example, = matches "=" ; @ matches "@" .

How do I match a number in regex?

To match any number from 0 to 9 we use \d in regex. It will match any single digit number from 0 to 9. \d means [0-9] or match any number from 0 to 9. Instead of writing 0123456789 the shorthand version is [0-9] where [] is used for character range.

How do I match a range in regex?

The regex [0-9] matches single-digit numbers 0 to 9. [1-9][0-9] matches double-digit numbers 10 to 99. Something like ^[2-9][1-6]$ matches 21 or even 96! Any help would be appreciated.

What is regex AZ match?

The regular expression [A-Z][a-z]* matches any sequence of letters that starts with an uppercase letter and is followed by zero or more lowercase letters.


1 Answers

^[A-Za-z0-9_.]+$ 

From beginning until the end of the string, match one or more of these characters.

Edit:

Note that ^ and $ match the beginning and the end of a line. When multiline is enabled, this can mean that one line matches, but not the complete string.

Use \A for the beginning of the string, and \z for the end.

See for example: http://msdn.microsoft.com/en-us/library/h5181w5w(v=vs.110).aspx

like image 155
Ikke Avatar answered Oct 02 '22 15:10

Ikke