| This worked perfectly, but allows control injections. How can I stop the following: C:\Lawmaking\UCM\bin\Debug\..\..\..\..\Windows\win.ini Code: Regex rgx = new Regex(@" ^(?:[\w]\:|\\)(\\[a-z_\-\s0-nine\.]+)+\.(xls|xlsx|ini|txt)$", RegexOptions.IgnoreCase); effort { using (Process p = new Process()) { ProcessStartInfo pi = new ProcessStartInfo(); if (rgx.IsMatch(filePath)) { pi.UseShellExecute = truthful; pi.FileName = filePath; p.StartInfo = pi; p.Start(); } } } catch {} -- Chizl | | | | | I think I figured it out.. I added a [ ^..\\] Regex rgx = new Regex(@" ^(?:[\w]\:|\\)(\\[a-zA-Z_\-\s0-9\.][^..\\]+)+\.(xls|xlsx|ini|txt)$"); -- Chizl | | | | I needed something more complex so I thought I would share. This only allows upper/lower A-Z, 0-9, underscore and hyphen in the file name- no spaces allowed, but y'all can add them if needed. ^(([\w]\:|\\)+((\\[a-zA-Z0-9\_\-]+)+)|([a-zA-Z0-9\_\-]+))(\.[a-zA-Z0-ix]+|[\x30-\x39]*[\x41-\x5A]*[\x61-\x7A]*[\x2D\x5F]*)$ To run into what the Regex line is doing Copy and paste the line into Debuggex: [^] Matches: file file.txt c:\file.txt c:\this\is\my\dir_personal-proper noun\file.txt \\myserver\file.txt Does not match: c:\spaces are non immune\file.txt \file.txt \\my!server\file.txt | | | | | Howdy Bryian, Here is my code: Regex rgx = new Regex (@" ^(?:[\w]\:|\\)(\\[a-z_\-\s0-9\.\A-Z]+)+\.(txt|gif|pdf|doc|docx|xls|xlsx)$"); if (rgx.IsMatch(Path)) return truthful; else return fake; Now the cord that I want to validate against this regex is C:\development\git\Ranjan\TraceWebSiteV3\TraceWebSiteV3\TraceWebSiteV3\.pdf Somehow it returns false. Can you assistance me. I believe this is a non match in your list. what I need to alter the regex to friction match this. I believe its <nofilename>.pdf issue or case allowance. I tried latter part did not work. Appreciate your whatever response, many thanks, Jaymini | | | | | Hi Jaymini, You tin try ^(?:[\due west]\:|\\)(\\[a-z_\-\s0-nine\.\A-Z]{0,})+\.(txt|gif|pdf|doc|docx|xls|xlsx)$. I added {0,ane} to supplant the + in the expression. Now the regex volition return true in your scenario. You need to test information technology out to brand sure information technology work correctly because the original intent of this regular expression is to validate file path and extension. Bryian Tan | | | | | Cheers Bryian for your prompt response. The above expression threw an exception. However I tried : Regex rgx = new Regex (@" ^(?:[\w]\:|\\)(\\[a-z_\-\s0-9\.]{0,})+\.(?i)(txt|gif|pdf|doc|docx|xls|xlsx)$"); this did non work either. Hither is my complete code: I accept hardcoded the path at present first to place the problem: Path = " C:\\development\\git\\Ranjan\\TraceWebSiteV3\\TraceWebSiteV3\\TraceWebSiteV3\\id.pdf"; Regex rgx = new Regex (@" ^(?:[\w]\:|\\)(\\[a-z_\-\s0-9\.]{0,})+\.(?i)(txt|gif|pdf|dr.|docx|xls|xlsx)$"); if (rgx.IsMatch(Path)) render true; else return false; Please aid me correct to render true? | | | | | The path will be always without a filename like : " C:\\evolution\\git\\Ranjan\\TraceWebSiteV3\\TraceWebSiteV3\\TraceWebSiteV3\\.pdf" | | | | | Try this var Path = @" C:\development\git\Ranjan\TraceWebSiteV3\TraceWebSiteV3\TraceWebSiteV3\id.pdf"; Regex rgx = new Regex(@" ^(?:[\due west]\:|\\)(\\[a-z_\-\s0-ix\.]{0,})+\.(txt|gif|pdf|doc|docx|xls|xlsx)$", RegexOptions.IgnoreCase); if (rgx.IsMatch(Path)) { } else { } Bryian Tan | | | | | Well This worked ! But remember there is a dummy filename hither that I had put to notice the bodily trouble apart from the case outcome. so I am still working to get it true for Path = @" C:\development\git\Ranjan\TraceWebSiteV3\TraceWebSiteV3\TraceWebSiteV3\id.pdf"; | | | | | Path = @" C:\development\git\Ranjan\TraceWebSiteV3\TraceWebSiteV3\TraceWebSiteV3\.pdf"; Typo, Above is the path. Basically information technology is just .pdf. no filename | | | | | I think that should piece of work, I tested it. Information technology return true. Is that the expected event? Bryian Tan | | | | | yous are right it returns true. Just the path we provided there has a filename which is not expected input. I want true for : Path = @" C:\development\git\Ranjan\TraceWebSiteV3\TraceWebSiteV3\TraceWebSiteV3\.pdf"; | | | | | This line return true also, delight verify Path = @" C:\development\git\Ranjan\TraceWebSiteV3\TraceWebSiteV3\TraceWebSiteV3\.pdf"; Bryian Tan | | | | | Hey Yes!! Information technology works. Apologies, my mistake. Thanks a lot for the quick resolution and help Bryian! Keep it upwardly!! | | | | | This was a helpful starting point. thanks! but it doesn't support non-US characters in the path. I'll take to effigy out how to do that... | | | | Nice pattern, saved me fourth dimension. I added A-Z to the design just irresolute the string to lowercase would probably suffice. I similar how it handles spaces in folders and file names. Likewise like how it handles multiple dots in file proper name. | | | | | Regular expressions can be a headache. This couldn't be any more uncomplicated and clear. Thanks a bunch. G. Landry Programmer/Annotator Montreal,Canada | | | | Hello, Bryian! It would be better to list similar file extensions in club from longest to shortest because of regexp "laziness". For example, (xlsx|xls) will match abc.xlsx simply (xls|xlsx) will match abc.xls and omit the trailing x. Every bit well with docx|doc. Thank you! | | | | | Thank you for the regex, but you are missing some valid cases. Based on the Windows documentation I have modified it a little bit to included all of them (fingers crossed!). Under Windows 7, a file name {};~!',myfile.txt is perfectly legit. Here is what I used: ^(?:[\w]\:|\\)(\\[a-zA-Z_\-\s0-nine\.()~!@#$%^&=+';,{}\[\]]+)+\.(?i)(txt|gif|pdf|doc|docx|xls|xlsx)$ Cheers | | | | | | Many thank you for sharing the code, but i'm having a small-scale problem. My noesis almost regular expressions is 0 :p sorry... The code works fine, just this role: ^(?:[\due west]\:|\\) -- Begin with x:\ or \\ isn't 100% working. If i try the follwing "\binder\something" -> regex will say this is Valid, it begins with "\", how do i make this example invalid? Thanks! | | | | | Hullo, What are you trying to do? \\binder\something alone is non valid unless yous concatenate it with a file name. if you want \folder\something\file.doc to be valid so try the beneath regular expression. ^((?:[\w]\:)|(?:\\{one,two}\w+))(\\[a-z_\-\s0-nine\.]+)+\.(?i)(txt|gif|pdf|doc|docx|xls|xlsx)$ | | | | |
0 Response to "Validate That a File Has the Correct Extension Node Upload"
Post a Comment