regex to get portion of file name after last dot without file extension - regex

I have a bunch of files, some examples are as follows:
/foo1/foo2/bar1.bar2.bar3.answer.jar
/foo1/bar1.bar2.answer.jar
/foo1/foo2/answer.jar
and for all of the above I would like a regex that matches 'answer'. In other words, I'm looking to get an alias for the file that is the portion of the file name after the last dot (or the file name itself if there are no dots) with the file extension (.jar can be guaranteed here to make it simpler) stripped off.
I know I can do this with a more simple regex to split the value up by dots and then get the second last one, but in this case I'm building a back-end thing that will ideally take a regex that is defined in a configuration definition for the given file type, and spit out the alias, which might be different for other file types.
Yep, I'm over-engineering. :)
Any ideas?

Following regex should work for you:
[^/.]+(?=\.jar$)
If using Javascript or a similar flavor where / is regex delimiter then you need to escape / like this:
[^\/.]+(?=\.jar$)

You can use the following regexp: (assuming that the answer part doesn't contain . or /)
[/\.]([^/\.]+)\.jar
The first capturing group is the part what you want to.

Related

Extract a filename from json file path

1. env\nonprod\alert\prj-data-02\alt\airflow-alert.json
2. env\nonprod\alert\prj-data-02\alt\biquery-alert.json
I would like to use regex to get the word before "*-alert.json" in the line. For example: from line 1 I would like to get the name 'airflow' and from line 2 I would like to get the value 'bigquery'.
.*\\(.*)-alert\.json$ should work. The parentheses mean it captures the words in them. There are ways of accessing whats contained in them but it depends on the implementation of regex that you are using. I think bash uses $<index> syntax for capture groups in pipelines and such. Here’s a website that describes this.

NotePad++ regex match and replace and also keep match to convert to different markdown image reference link

I have the following link syntax that needs to be changed:
![[afoldernamenolongerneededandwillbedeprecated/somemarkdownfilename_image1.png]]
I tried (successfully) with this regex to match:
![[].*[\/].*_image[0-9].png[]]]
Although I have a hunch it may not be what I should use. I the novice think it may be only good for matching and not replacing. All images are png's, by the way. All filenames have _image in them, prefixed by the markdown file-name.
Desired end format:
![image](imagenamefromabovestring1,2,orhowevermanythereare.png)
The
![]()
is a known syntax in markdown to reference images. Images will be populated in subdirectories the program/app will find.
It goes without saying I want to run find and replace recursively on some 4000 files containing image references.
I put up the unfinished substitution example here:
https://regex101.com/r/Bl8HJC/1
So to clarify more on what I need. I need the formerly present folder name gone. I don't need it anymore. Then after the slash comes the name of the image, the syntax of which is always: current filename to be proccessed by NotePad++ recursively (it can be a markdown file named Ab, Aba, Abracadabra, etc.) and this filename always serves as prefix, then comes an underscore and 'image' + a number depending on how many images are linked to the markdown file as attachments. The names of the files to go in an attachment folder will look like this:
AB_image3.png
Abracadabra_image2.png
.
.
.
Zodiac_image45.png
I am looking for the right syntax as I couldn't figure it out with the dollar sign.
Cheers,
Otto
I have modified your example to get it working here. What you needed to do is escape the square brackets so they would be interpreted literally, since they have special meaning in regex, and you needed to use a capture group to store the matching value in $1 so you could use it in the replacement.
Regular expression:
!\[\[.*\/(.*_image[0-9]{1,2}\.png)\]\]
Substitution format:
![image]\($1\)
Edit: Question was revised to state that the folder name was unwanted in the final output, so matches are delimited after the final / character in the file path.
Edit 2: Support for file numbers 1 through 99.

regex match file name in a path

I am trying to match the file name in a path. For example:
/path/test/index.html
I would want to match index.html
However I also can have a path with no / so the path could be
index.html
and would want to match index.html
I have the following to match the first case and can grab it with a group.
.*/([^/]+)
But how can I also match a file name when the only thing in the path is the file name?
There is probably no need to have anything but [^/]+$ unless you want to
match the entire line and your engine matcher requires it.
There is usually more than one way to do a regex, and also that there are often edge cases that end up complicating a simple task.
If you want to match any/ every valid file name in a string then perhaps:
[A-Za-z0-9_-]+\.?[A-Za-z0-9]*
or (since you can have a file named ConfigurationFile.txt.bac for example)...
[A-Za-z0-9_-\.]+\.?[A-Za-z0-9]*
But that is not what you want because each directory name is a valid file name... so...
this will match only valid file names with an extension.
[A-Za-z0-9_-]+\.[A-Za-z0-9]+
or
[A-Za-z0-9_-\.]+\.[A-Za-z0-9]+
Clearly there are many options. The AA(accepted answer) only matches any string in a path that is at the end of the path. It does not match a file name without a path. The AA may well do for the OP. It is helpful to me to be able to match any file name within a string.
There are always edge cases, for example in my case I am still matching version numbers with this regex. I have a work around for my case but I am getting too specific.
Make the .*/ into an optional group:
(?:.*/)?([^/]+)

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-.+/

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
.