last day (14 days later) » 

1:53 PM
1
Q: Doing some pattern matching

georgemMy goal is. I have some files like: file1.txt, file2.bmp, file1.jpg, file1.gif, pic.bin. User must enter Regex string in text box. I must filter file names using this regex. But I don't care about extension. Any extension is allowed. So I have this final regex: string finalRegex = textBoxUserE...

 
As a user, I will be confused that $ doesn't work as expected...
 
Wouldn't it be easier to match against the filename with its extension removed? This would avoid any interference from the user entered part of the regex (eg. they've added a $ at the end).
 
@Athari: It's not really a user, say "advanced" user. But what do you mean? Please bear in mind I am beginner with Regex, that is why I asked this question, and I don't get what you mean exactly
 
An advanced user is even more likely to use $ themselves.
 
@georgem User enters ^foo$. He thinks it will accept only "foo", but in fact it will require a dollar sign in the name.
 
1:53 PM
There is a convenient method you could use. System.IO.Path.GetFileNameWithoutExtension and the jsut apply user-entered regex to the result of this method.
 
@Richard: I have array of filenames, e.g., file1.png, file2.jpg, file3. You suggest I remove extensions? It may become tricky because when regex returns filenames (without extension), how do I know, which file initially it corresponded to? Maybe two files had same name but different extension?
@Maku: I know see my previous comment. But when regex returns filenames without extension, I need to reconstruct my old files (now, with extension). Where do I store, which file had which extension for example?
 
Why would you store file name extracted by regex instead of just original filename? Why not just test file name for match and store it instead of the actual match from regex?
 
You need to call the regex method, you keep the original filename when passing name without extension. (Eg. theRegex.IsMatch(Path.GetFileNameWIthoutExtension(theFileName)))
 
@Richard: Keep in mind I may have the possibility to warn users for example not to type $ for example. What in this case?
 
Always assume at least some users will ignore anything you write (this is why input validation is needed).
 
1:53 PM
@Richard: not here, this is not standard application. It will not be used by many users. And they won't do bad things deliberately. In this case? Because I have hard time following/understanding your suggestion. Like I said, what to do if there were 2 files with same names, but different extensions?
 
var files = names.Where(path => reg.IsMatch(Path.GetFileNameWIthoutExtension(path))).ToArray(); Where do you see an ambiguity here? Path.GetFileNameWIthoutExtension(string str) does not change the contents of str if that's you worry about.
 
You will learn, or shift careers, that users do not read or follow the instructions. Whatever subset of users you choose. Sometimes it will be in error or they're in a rush.
 
@Richard: Then I don't understand how your or westons solution deals with issue when there are two files with same name but different extension????.
@Richard: And what's wrong with $ being there? I am new to Regex like I said.
 
Are you sure you understand in depth how your code works (especially the LINQ part) and what is the output in var files?
 
$ matches end of string. So something$else will never match, because you cannot have text beyond the end of the string. (Ignoring newlines because filenames don't have them.)
 
1:53 PM
@Richard: How would my date query string look like with westons solution? And can you please give 1 two sample queries I can test? (like if file starts with some letter query).
@Maku: how would regex date query (the one in my question) look like now, with weston's solution applied?
Hello, can someone help?
This is my code now:
Regex regex = new Regex(regexString);//names contains filenames including extension
string[] files = names.Where(s => regex.IsMatch(Path.GetFileNameWithoutExtension(s)))
.Select(s => s).ToArray();
When user entered this string: "p.*?" in text box
I am getting [0] = "C:\\testpic.bmp"
Should be
[0] = "C:\\test\\pic.bmp"
this
Can someone please help?
 

last day (14 days later) »