Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for english characters, hyphen and underscore

Tags:

c#

regex

I need Regex for english characters, hyphen and underscore

Example

Match :

govind-malviya
govind_malviya
govind123
govind

Not Match

govind malviya
govind.malviya
govind%malviya
腕錶生活
вкусно-же
like image 788
Govind Malviya Avatar asked Feb 02 '13 06:02

Govind Malviya


People also ask

How do you allow 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.

What is the regex for underscore?

The _ (underscore) character in the regular expression means that the zone name must have an underscore immediately following the alphanumeric string matched by the preceding brackets. The . (period) matches any character (a wildcard).

Is underscore special in regex?

Regex doesn't recognize underscore as special character.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).


1 Answers

try this out:

^[A-Za-z\d_-]+$

A-Za-z would allow alphabets.
\d would allow numbers.
_ would allow underscore.
- would allow hyphen. ^ and $ represent the start and end of string respectively.

like image 85
Ali Shah Ahmed Avatar answered Sep 23 '22 01:09

Ali Shah Ahmed