Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Regex for Windows Domain Username in C#?

Tags:

regex

c#-4.0

What is the regular expression to accept only text, number and backslash. It should not accept space and should start with text only. For example domain\username. Thanks in advance...

like image 424
Ashok Ranjith Avatar asked Mar 14 '23 12:03

Ashok Ranjith


1 Answers

this is a regex for domain\name with the restriction that 'domain' should start with a char and end with a char. You can easily maniplate the regex for your desire

/^[a-zA-Z][a-zA-Z0-9-]{1,61}[a-zA-Z]\.[a-zA-Z]{2,}$/

Domain - Beginning:

    [a-zA-Z] Text

Domain - Text:

   1-61 times of [a-zA-Z0-9-] Text, Numbers, '-'

Domain - End:

   1 time [a-zA-Z] = Text

Backslash:

   1 time [\]

User - Text:

   2-infinity times [a-zA-Z] = Text

Edit: as bgh pointed out in the comment you could include more valid characters

/^[a-zA-Z][a-zA-Z0-9‌​\-\.]{0,61}[a-zA-Z]\\\w‌​[\w\.\- ]*$/
like image 69
Marc Wittmann Avatar answered Mar 17 '23 01:03

Marc Wittmann