How do you find a "."?

I'm trying to create a regular expression to look for filenames from full file paths, but it should not return for just a directory. For example, C:\Users\IgneusJotunn\Desktop\file.doc should return file.doc while C:\Users\IgneusJotunn\Desktop\folder should find no matches. These are all Word or Excel files, but I prefer not to rely on that. This:

StringRegExp($string, "[^\\]*\z",1) 

finds whatever is after the last slash, but can't differentiate files from folders. This:

StringRegExp($string, "[^\\]*[dx][ol][cs]\z",1)

almost works, but is an ugly hack and there may be docx or xlsx files. Plus, files could be named like MyNamesDoc.doc. Easily solved if I could search for a period, but . is a used character (it means any single character except a newline) which does not seem to work with escapes. This:

StringRegExp($ue_string, "[^\\]*\..*\z",1)

should work, finding anything after the last backslash, capturing only something with a period in it. How to incorporate a period? Or any way to just match files?

Edit: Answered my own question. I'm interested in why it wasn't working and if there's a more elegant solution.


Local $string = StringRegExp($string, "[^\\]*\.doc\z|[^\\]*\.docx\z|[^\\]*\.xls\z|[^\\]*\.xlsx\z",1)

Periods do in fact work with the same escape slash most special characters use. As for the document type, an Or pipe and a different extension works great. If for some reason you need to add an extension, just add another Or.

Meh, I'm bored. You could do this:

$sFile = StringRegExp($sPath, "[^\\]+\.(?:doc|xls)x?$", 1)

There's no guarantees that a folder wouldn't be named that, so to be absolutely certain you'd have to check the file/folder attributes. However it's doubtful anyone would name a folder with something like '.docx'

  • Reverse the string.
  • Look for the "."
  • Look for "\" with StringInStr (and/or "/")
  • Trim the right side from the return of StringinStr
  • Reverse it again.

Need Your Help

Correct pdo connection to use accents

mysql pdo non-ascii-characters

Hi I have a problem displaying accents on a PDO connection.

How can I get the date of the day 20 minutes earlier in SSIS reliably?

sql-server date variables datetime ssis

I'm working on a project in SSIS that takes cost and assigns it to a certain week. The weeks are identified by the date of the next Sunday. I run the package every 20 minutes. So at 12:00:47-ish...