• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 400
  • Last Modified:

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

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
John500
Asked:
John500
  • 3
  • 3
1 Solution
 
John500Author Commented:
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
 
John500Author Commented:
Forgot the file:
regular-expression.JPG
0
 
Fernando SotoCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Fernando SotoCommented:
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
 
John500Author Commented:
Fernando,

Thanks much for the detailed answer.

That's what I needed .... !
0
 
Fernando SotoCommented:
Not a problem, always glad to help.  ;=)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now