Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to match alphanumeric, hyphen, underscore and space string

Tags:

regex

I'm trying to match a string that contains alphanumeric, hyphen, underscore and space.

Hyphen, underscore, space and numbers are optional, but the first and last characters must be letters.

For example, these should all match:

abc
abc def
abc123
ab_cd
ab-cd

I tried this:

^[a-zA-Z0-9-_ ]+$

but it matches with space, underscore or hyphen at the start/end, but it should only allow in between.

like image 446
Muddu Patil Avatar asked Jan 21 '16 06:01

Muddu Patil


People also ask

How do you match a hyphen in regex?

In regular expressions, the hyphen ("-") notation has special meaning; it indicates a range that would match any number from 0 to 9. As a result, you must escape the "-" character with a forward slash ("\") when matching the literal hyphens in a social security number.

How do you write alphanumeric in regex?

The regex \w is equivalent to [A-Za-z0-9_] , matches alphanumeric characters and underscore.

What does \b mean in regular expressions?

Simply put: \b allows you to perform a “whole words only” search using a regular expression in the form of \bword\b. A “word character” is a character that can be used to form words. All characters that are not “word characters” are “non-word characters”.

How do I match a character in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).


2 Answers

Use a simple character class wrapped with letter chars:

^[a-zA-Z]([\w -]*[a-zA-Z])?$

This matches input that starts and ends with a letter, including just a single letter.

There is a bug in your regex: You have the hyphen in the middle of your characters, which makes it a character range. ie [9-_] means "every char between 9 and _ inclusive.

If you want a literal dash in a character class, put it first or last or escape it.

Also, prefer the use of \w "word character", which is all letters and numbers and the underscore in preference to [a-zA-Z0-9_] - it's easier to type and read.

like image 67
Bohemian Avatar answered Oct 10 '22 03:10

Bohemian


Check this working in fiddle http://refiddle.com/refiddles/56a07cec75622d3ff7c10000

This will fix the issue

 ^[a-zA-Z]+[a-zA-Z0-9-_ ]*[a-zA-Z0-9]$
like image 42
Thanga Avatar answered Oct 10 '22 03:10

Thanga