Home > Software engineering >  NotePad regex match and replace and also keep match to convert to different markdown image referen
NotePad regex match and replace and also keep match to convert to different markdown image referen

Time:08-04

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_image1.png
Abracadabra_image2.png
.
.
.
Zodiac_image99.png

I am looking for the right syntax as I couldn't figure it out with the dollar sign.

Cheers,

Otto

CodePudding user response:

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.

  • Related