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 !
4 Answers
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.
2 Comments
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
D:\one\two we need to make a change to this...also, are you sure you just want lower case foldernames?/^[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
[\]should probably be \\ as well