Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Regular expression for Windows Domain user name

Last post 06-04-2008, 7:57 PM by Aussie Susan. 19 replies.
Page 1 of 2 (20 items)   1 2 Next >
Sort Posts: Previous Next
  •  04-28-2008, 8:07 AM 41682

    Regular expression for Windows Domain user name

    Hi, 

    I have running my .Net application and I need a Regular expression for valid windows domain user name.

    Example : DomainName\UserName (domain name followed by only one backward slash and followed by username)

    As per standard criteria windows domain name :-

    1.Can  have alphanumberic (letter + number) but shouldn;t start with numbers. 2.No special character except dotnet (.) and hyphen (-)

    As per standard Criteria for windows user name :-

    1.can have alphanumberic (letter + number ) excpet following special Characters

    \ / " [ ] : | < > + = ; , ? * @ 

    it can have space but user name should not be full of spaces ( we will ignore it for time being )

     Please any body give a regular expression for windows domain user name.

    Thanks,

  •  04-28-2008, 8:16 PM 41705 in reply to 41682

    Re: Regular expression for Windows Domain user name

    If I understand you correctly, you are only interested in the user name part and not the domain name. Given your rules for the domain name (specifically that it cannot contain a special character of '\') then:

    \\(?! +)([^\\/"[\]:|<>+=;,?*@]+)

    will locate the '\' between the domain and user name parts, and then check that whatever follows to the end of the line is not one of the special characters and is not all spaces.

    By the way, your criteria for the user name is a little confusing because the alphanumeric character set does not contain any of those prohibited characters. Therefore I've interpreted this as 'any character except those listed".

    If I have missed something then please let me know.

    Susan 

  •  04-29-2008, 12:21 AM 41717 in reply to 41705

    Re: Regular expression for Windows Domain user name

    Sorry if i have not explained it propertly.

    I am interested in both username and domain name.

    for example valid userdomain name will be scott\tiger where scott is domain name and tiger is user name and "\" between them.

    when user input user domain name ,i need to validate whether he has inputted in proper format or not.So i need regular expression for that.

    Since user name and domain name have their own restriction ,i have just mentioned again as below :


    As per standard criteria windows domain name :-

    1.Can  have alphanumberic (letter + number), but shouldn;t start with numbers. 2.No special character except dotnet (.) and hyphen (-)

    As per standard Criteria for windows user name :-

    1.can have alphanumberic (letter + number ), except following special Characters

    \ / " [ ] : | < > + = ; , ? * @

    user name can have space but it should not be full of spaces.


    I hope  this time i have  framed my query clearly.I am just trying to get regex expression for valid domain user name (scott\tiger)

    Plz give us any regular expression for same.

  •  04-29-2008, 7:18 PM 41754 in reply to 41717

    Re: Regular expression for Windows Domain user name

    OK, try:

    ^([a-z][a-z0-9.-]+)\\((?! +$)[a-z0-9 ]+)$

    with the 'ignore case' option set. Note that there is a space in the "(?! +$)" and "[a-z0-9 ]"  subpatterns.

    If the text you are examining contains a <CR><LF> terminator, then you will need

    ^([a-z][a-z0-9.-]+)\\((?! +\r?$)[a-z0-9 ]+)\r?$

    to cater for the <CR> part. Using the test strings

    scott\tiger
    domain3.name\my name
    dumbname\0349
    2many\tiger
    scott\   
    dumb$name\0349

    the above matches the first 3 and rejects the last 3. (Again, there are spaces after the "scott\    " example text).

    I am still confused by your requirement for the user name because alphanumerics (as you say, these are letters such as A,B,C through to Z, and the numbers 0 to 9) don't have any of the special characters. Therefore I don;t know if you really mean that those characters are acceptable as well as the aphanumerics, or that you want any printable character except those special characters. In  either case, between the two patterns I have given you, you should be able to work out how they work and how to extend them.

    Susan

     

  •  04-30-2008, 11:03 AM 41779 in reply to 41754

    Re: Regular expression for Windows Domain user name

    Hi,

    Thanks for the reply.Expression You wrote for 'domain' name part is fine.I ma not sure about the 'user name' part.

    let me explain you the criteria for user name part only.

    As per standard Criteria for windows user name :-

    1.user name can have alphanumberic (combination of letters + numbers ).Even user name can start with numbers followed by letters too.

    2. user name should NOT contain special character.Those special characters are mentioned below :

    \ / " [ ] : | < > + = ; , ? * @

    user name can have space but it should not be full of spaces

    I hope this time i framed it in understandable way.

    In case if u find it confusing again regarding user name criteria, You just try to create a local user ( using Computer Management Tool)  in windows by inputting any of above special characters in user name text box.U will get a error message window, with criteria mentioning for valid user name.

    thanks,

     

     

  •  04-30-2008, 7:09 PM 41817 in reply to 41779

    Re: Regular expression for Windows Domain user name

    In that case, put the two patterns I have given you together:

    ^([a-z][a-z0-9.-]+)\\(?! +)([^\\/"[\]:|<>+=;,?*@]+)$

    I generally find that, if someone is having trouble understanding what I say (not an uncommon experience I might add!!!) then simply repeating what I said before does not really help. Also the only windows system I have access to is at work and it is 'locked down' so that I do not have access to anything other than pre-installed programs.

    Therefore, if the above does not help you, then please let me know of the exact rules.

    Susan
     

  •  05-04-2008, 7:48 AM 41887 in reply to 41817

    Re: Regular expression for Windows Domain user name

    thanks for reply,

     i am getting compile time error.

    I tested ur regular expression using a tool called "Expresso Regular Expression Development Tool" ,i am getting compilation error.

     

    error message :

     "Missed Placed Quantifier one or more repetition" for below part of regular expression

    ^([a-z][a-z0-9.-]+)\\(?! +)([^\\/"[\]:|<>+=;,?*@]+)$

    can u please let me know what is problem.u can download tool by google it and it is free for demo purpose.

     

    thanks,

     

  •  05-04-2008, 8:59 AM 41889 in reply to 41887

    Re: Regular expression for Windows Domain user name

    try to parse this in Expresso

    ^([a-z][a-z0-9.-]+)\\(?!\x20+)([^\\/"[\]:|<>+=;,?*@]+)$

    i parsed it OK. Same thing, used \x20 instead of a white space.

  •  05-04-2008, 9:56 PM 41900 in reply to 41887

    Re: Regular expression for Windows Domain user name

    I use Expresso all the time and the pattern I provided was tested using it.

    The problem is most likely that Expresso has the 'ignore whitespace' option on by default (if I recall correctly) and so it will tell the regex engine to skip over the space in the pattern I provided. Uncheck this option and it will parse correctly. Sergei's solution gets around this by using an alternative mechanism to define a character.

    By the way, when used in your program, the 'ignore whitespace' option (if it exists in the regex engine you are using) will be off by default so this will not be an issue.

    Susan 

  •  05-07-2008, 1:44 AM 41975 in reply to 41900

    Re: Regular expression for Windows Domain user name

     

    hi thanks for reply.

    I tested expression throughly for all kinds of patterns.

    with respective to domain name concerned,it is working perfectly.

    But I left out one more condition for user name .Let me put condition once again.

    Condition Text written in Green Color   point # 2(below) was missing in my earlier condition list.

    1.   Alphanumeric, Can start with numbers. - done


     2 . Name may not consist entirely of periods and /or spaces, or contain following characters:
       \ / " [ ] : | < > + = ; , ? * @

     2 . Name may not consist entirely of periods and /or spaces, or contain following characters:
       \ / " [ ] : | < > + = ; , ? * @

    When we try to create just user in windows with some invalid characters,i get error message which is same as point # 2.

    So i missed sub condition in point # 2 marked in Green Color  

    So What changes to be done for earlier regular expression to meet missed condition as well along with other conditions.

    Please help me out.

    thanks

  •  05-07-2008, 7:10 PM 42001 in reply to 41975

    Re: Regular expression for Windows Domain user name

    Try:

    ^([a-z][a-z0-9.-]+)\\(?![\x20.]+$)([^\\/"[\]:|<>+=;,?*@]+)$

    The key here is the sub-pattern '(?![\x20.]+$)' which scans ahead matching one or more period or space characters followed by the end of the text - if that matches then the negative lookahead fails and so the whole pattern fails. Only if some character other than a space or period is found will the rest of the pattern be tried which then checks for the other valid (or invalid) characters. If there are other characters that fall into this category then they can be added in here as well.

    Note that the '$' has been added to the pattern proposed above to stop failing on examples such as "asd\....x" which, according to your rules is valid as it is not "entirely" periods.

    Susan 

  •  05-13-2008, 12:40 AM 42159 in reply to 42001

    Re: Regular expression for Windows Domain user name

    HI

     thanks for reply..I tested Your Expresssion using expresso...it is working fine...

    but when i tried to use expression in .NET (C#) ,i am getting runtime error ...

    My code is as below.... I have use couple of "\" escape character for " and \ in expression as it was not throwing me compile time error.

     

    Error Message :

    {"parsing \"^([a-z][a-z0-9.-]+)\\(?![ .]+$)([^\\/\"[\\]:|<>+=;,?*@]+)$\" - Too many )'s."}

    CODE (C#) :

    private const string ValidUserDomainNameRegularExpression = "^([a-z][a-z0-9.-]+)\\(?![\x20.]+$)([^\\/\"[\\]:|<>+=;,?*@]+)$";

    private Regex _regularExpression;

    private bool IsValidUserDomainName(string userDomainName)  
     {

         _regularExpression = new Regex(ValidUserDomainNameRegularExpression, RegexOptions.IgnorePatternWhitespace);
          if(_regularExpression.IsMatch(userDomainName))
          {
             return true;
          }
         else
           {

            return false;

            }

      }

     

    If you have any idea about this ,please let me know.

    Thanks

  •  05-13-2008, 12:51 PM 42191 in reply to 42159

    Re: Regular expression for Windows Domain user name

    Looking at Susan's regex and what you are using in your code it does not look like you escaped all the backslashes. Every backslash in the pattern has to be escaped if you are are going to take the escaping approach.

    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  05-13-2008, 8:35 PM 42220 in reply to 42159

    Re: Regular expression for Windows Domain user name

    I would suggest using the @"    " string form in C# - this gets around some of the escaping issues (except double-quotes).

    Also, off the subject entirely, but instead of:

          if(_regularExpression.IsMatch(userDomainName))
          {
             return true;
          }
         else
           {
            return false;
            }

    why not:

    return _regularExpression.IsMatch(userDomainName);

    Susan 

  •  05-14-2008, 12:29 AM 42230 in reply to 42220

    Re: Regular expression for Windows Domain user name

     

    I used  @" " ..but didn;t work for me.

     I getting error in line just above the if statment...So it doesn't matter whether we use return _regularExpression.IsMatch(userDomainName);
     or with if statment.

    I used escape character for all "\" ..but it didn;t help me.

    Somebody who know .NET,please try this expression and let me know what is solution for error.

    Thanks,

Page 1 of 2 (20 items)   1 2 Next >
View as RSS news feed in XML