Regex for file name in a directory - regex

I have two files in a directory. FileAbc_1.xml and FileAbc.xml. I want write a regex that only select FileAbc_1.xml.
My regex is : FileAbc.*.xml
It is picking up both file names but I only want FileAbc_1.xml. Any help would great favor.

This will work for you
FileAbc_[0-9]+.xml

That should just be: FileAbc_\d\.xml
(assuming there's never more than one digit after the underscore)

You can go with this for anything that will start with FileAbc and end with XML FileAbc.+\.xml.

Related

Regex in file names

I use free little program Metamorphose for changing file names.
Problem is I need to use regex to change names in order as shown below:
Find: nice-tree-([\s\S]*?)
Replace: nice-tree-$1-abc
As you can see all files that start with nice-tree-ANYTHING should be replaced with -abc at the end of every file name.
I'm everything just not expert for regex usage...
Both of you were right. It works now. Thanks.
Assuming you don't need to replace the full file name with the extension. The following regex would match all the file names that you are looking for.
/nice-tree-.+/

Find all file names that match a pattern

I am trying to find a way to list all file names in a folder that matches this pattern :
20131106XXXXX.pdf
The prefix is the date, and the content and length of XXXX vary across files, and I only care about pdf files.
Anyone could advise a way to do this?
Try this
list.files(path="./yourdir",pattern="[[:digit:]]{8}.*\\.pdf")
You can use regex.
files <- dir(pattern="^[0-9]{8}.*\\.pdf")

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.

Regex: how do I capture the file extension?

How do I determine the file extension of a file name string?
lets say I have
I'm.a.file.name.tXt
the regex should return tXt
something like \.[^.]*$ should do it
You probably don't need regex - most languages will have the equivalent to this:
ListLast(Filename,'.')
(If you do need regex for some reason, Scharron's answer is correct.)
What language is this in? It's quite possible you don't want to actually use a regex - assuming the name is a String you'll probably just want to do something like split it over periods and then choose the last segment. Okay, that's sort of a regex answer, but not a proper one.
/^(.*?)\.(.*)$/
The '?' makes it greedy. Your result will be in the second group.

Regex: Get Filename Without Extension in One Shot?

I want to get just the filename using regex, so I've been trying simple things like
([^\.]*)
which of course work only if the filename has one extension. But if it is adfadsfads.blah.txt I just want adfadsfads.blah. How can I do this with regex?
In regards to David's question, 'why would you use regex' for this, the answer is, 'for fun.' In fact, the code I'm using is simple
length_of_ext = File.extname(filename).length
filename = filename[0,(filename.length-length_of_ext)]
but I like to learn regex whenever possible because it always comes up at Geek cocktail parties.
Try this:
(.+?)(\.[^.]*$|$)
This will:
Capture filenames that start with a dot (e.g. .logs is a file named .logs, not a file extension), which is common in Unix.
Gets everything but the last dot: foo.bar.jpeg gets you foo.bar.
Handles files with no dot: secret-letter gets you secret-letter.
Note: as commenter j_random_hacker suggested, this performs as advertised, but you might want to precede things with an anchor for readability purposes.
Everything followed by a dot followed by one or more characters that's not a dot, followed by the end-of-string:
(.+?)\.[^\.]+$
The everything-before-the-last-dot is grouped for easy retrieval.
If you aren't 100% sure every file will have an extension, try:
(.+?)(\.[^\.]+$|$)
how about 2 captures one for the end and one for the filename.
eg.
(.+?)(?:\.[^\.]*$|$)
^(.*)\\(.*)(\..*)$
Gets the Path without the last \
The file without extension
The the extension with a .
Examples:
c:\1\2\3\Books.accdb
(c:\1\2\3)(Books)(.accdb)
Does not support multiple . in file name
Does support . in file path
I realize this question is a bit outdated, however, I had some trouble finding a good source and wound up making the regex myself. To save whoever may find this time,
If you're looking for a ~standalone~ regex
This will match the extension without the dot
\w+(?![\.\w])
This will always match the file name if it has an extention
[\w\. ]+(?=[\.])
Ok, I am not sure why I would use regular expression for this. If I know for example that the string is a full filepath, then I would use another API to get the file name. Regular expressions are very powerfull but at the same time quite complex (you have just proved that by asking how to create such a simple regex). Somebody said: you had a problem that you decided to solve it using regular expressions. Now you have two problems.
Think again. If you are on .NET platform for example, then take a look at System.IO.Path class.
I used this pattern for simple search:
^\s*[^\.\W]+$
for this text:
file.ext
fileext
file.ext.ext
file.ext
fileext
It finds fileext in the second and last lines.
I applied it in a text tree view of a folder (with spaces as indents).
Just the name of the file, without path and suffix.
^.*[\\|\/](.+?)\.[^\.]+$
Try
(?<=[\\\w\d-:]*\\)([\w\d-:]*)(?=\.[\.\w\d-:]*)
Captures just the filename of any kind within an entire filepath. Purposefully excludes the file path and the file extension
Etc:
C:\Log\test\bin\fee105d1-5008-410c-be39-883e5e40a33d.pdf
Doesn't capture (C:\Log\test\bin)
Captures (fee105d1-5008-410c-be39-883e5e40a33d)
Doesn't capture (.pdf)
This RegExp works for me:
(.+(?=\..+$))|(.+[^\.])
Results (bold means match):
test.txt
test 234!.something123
.test
.test.txt
test.test2.txt
.