Solved

How to interpret:  "^\w+[\\]\w+$"

Posted on 2008-10-16
6
346 Views
Last Modified: 2012-05-05
Greetings:

I'm trying to debug code written by another developer.  What happens is the user is prompted to enter a domain and user name in order to proceed.  The code looks like this:

s.Owner = InputBox.Show(InputBox.IOptions.O_Cancel, ..., @"^\w+[\\]\w+$", @" Enter domain\username");

I'm guessing that after the user clicks the 'OK' button, the domain and user name are processed via the regular expression.  If I were to enter the following, what should be expected?

MyDomain\JoeUser

Thanks!
0
Comment
Question by:John500
  • 3
  • 3
6 Comments
 

Author Comment

by:John500
Comment Utility
Also, if I were to test this expression using the utility seen in the picture below, can anyone tell me what the correct steps are to test the expession?  I know I have to create the object, but then what?
0
 

Author Comment

by:John500
Comment Utility
Forgot the file:
regular-expression.JPG
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
Hi John500;

The InputBox control in your question must be a custom control, C# does not have such a form / dialog box that I can find and the version of the InputBox for Visual Basic has the syntax as followins:

Public Function InputBox( _
   ByVal Prompt As String, _
   Optional ByVal Title As String = "", _
   Optional ByVal DefaultResponse As String = "", _
   Optional ByVal XPos As Integer = -1, _
   Optional ByVal YPos As Integer = -1 _
) As String

 Which does not take strings as the last two parameters.

As far as the regex pattern, "^\w+[\\]\w+$", has the following meaning:

  1. ^ - States that the pattern will start matching from the very first character in the string
  2. \w - States to match any of the following characters a-z, A-Z, _ , 0-9
  3. + - Is a quantifier and states to match 1 or more of the previous characters
  4. [ ] - Encloses a character set and states to match any one of the character between [ and ]
  5. \\ - Because a \ is a meta-character in regex it needs to be escaped if it is one of the characters you wish to match so \\ is looking for a single \ in the input string
  6. $ - Matches the end of the string to be processed.
So the pattern, "^\w+[\\]\w+$", will match any string of character that starts with a a-z, A-Z, _ , 0-9 and 1 or more of the same characters followed by a \ followed by 1 or more of a-z, A-Z, _ , 0-9.

Will Match the following:
"MyDomain\JoeUser"
"My_Domain\JoeUser"
"MyDomain_1\JoeUser123"

Will NOT match the following:
"My-Domain\JoeUser"
"My@Domain\JoeUser"
"MyDomain_1\JoeUser#1"

Fernando
 
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
In your post using Regular Expression Demo program you do not see a result because the input string is invalid.

"\MyDomain\JoeUser"

The very first character \ causes it to fail because it is not defined by \w as being a first character. The following would be correct:


"MyDomain\JoeUser"
0
 

Author Comment

by:John500
Comment Utility
Fernando,

Thanks much for the detailed answer.

That's what I needed .... !
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Not a problem, always glad to help.  ;=)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now