0

i have a textbox in form. i want to write there in the textbox, the path (ex: D:\temp) and after that, i want to create this path. I tried to use this regex but is not working! i want the first letter be upper but is not working. Also i want the string contain ":" and "\" just once. And after ":\" i want to insert some string lowercase @"^[A-Z][:][\][a-z]$" Have some sugestions ? Thanks a lot !

7
  • Show what you have tried and isnt working. Commented Jul 22, 2016 at 13:35
  • 1
    [a-z] is only allowing one lower case character. Add + after it to allow 1 or more or use * for zero or more. And you need to delimit the backslash with a second one as it is currently delimiting the ]. Commented Jul 22, 2016 at 13:35
  • 1
    What do you mean with "it's not working"? What should work, what should fail? Give us examples! Commented Jul 22, 2016 at 13:35
  • 1
    [\] should probably be \\ as well Commented Jul 22, 2016 at 13:36
  • for example if i insert "a:\temp" this is correct... i want something like this "A:\temp" Commented Jul 22, 2016 at 13:39

4 Answers 4

2

I believe the regular expression you are looking for is

@"^[A-Z]:\\[a-z]+$"

There are two issues with your's. First you didn't delimit the backslash, so it was delimiting the ] which meant you were looking for exactly one ], [, or a through z after the colon. The second issue is that you want to find one or more letters after the backslash so you need to use + for that. Finally the colon and backslash do not have to be in groups.

Sign up to request clarification or add additional context in comments.

2 Comments

Based on the question, I also believe this is what is being asked for, but the OP should be aware that it won't match paths that have more than one nested directory. For example D:\temp\other will not match.
@fiveclubs It also won't match a file if it has an extension because it doesn't match the dot, and it won't match mixed case directory or file name or anything that has a non alpha character like underscore or a number.
1

You Can Try this:

[RegularExpression("^("[A-Z][a-z]+$)",
ErrorMessage = "Please Enter Correct Name")]

Description

  • ^ Beginning of string
  • [ ] Brackets specifies set of characters
  • A-Z a-z All capital/small letters
  • Consider a space
  • + one or more letters
  • $ Indicates end of string

Comments

0

Try this regex : [A-Z]:\[a-z]+. Then try ^[A-Z]:\\w+$

Declare your regex like this in your code :

string myRegex = @"[A-Z]:\\[a-z]+"

With this, you only validate paths like D:\Temp, but not D:\Temp\Temp2

Comments

0

First, examples of what should pass or fail would be helpful.

Second, use a tool to help write the regex that explains what's happening as you go, like Expresso or Regex Fiddle.

Now, on to the question. Here's an example /^[A-Z]:\\[a-z]+$/gm

  • ^ - It matches the start of a line
  • [A-Z] - matches a single capital letter
  • : - matches a colon
  • \ - escape the backslash to match a backslash
  • [a-z]+ - matches a lowercase letter (the + means 1 or more times)
  • $ - match the end of line

Here's an edited version which checks to ensure first character of folder is a capital letter /^[A-Z]:\\[A-Z][a-z]+$/gm

3 Comments

if you want multiple folders like D:\one\two we need to make a change to this...also, are you sure you just want lower case foldernames?
yes .. lowercase ... but what if the folder names should have the first letter upper? What should I do?
Then you have to add that to the regex before the lowercase stuff, like this /^[A-Z]:\\[A-Z][a-z]+$/gm I added a single check for a capital letter before the lowercase check...currently this regex will pass D:\Fo but fail for D:\F because it requires a capital letter and 1 or more lowercase. Changing the + to a * would change it to 0 or more lowercase letters

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.