I noticed that there are a lot of complex and interesting articles about Win cmd and Regex, but much less something simple, where you can begin to get acquainted with these powerful tools.
The idea of ​​writing this lesson arose when I realized that someone else was probably trying to solve the same problem, and perhaps such an article would help someone change the routine copy-and-paste for a much more interesting and technological method of writing code.
So, introductory:
A small site. I drive in there links to images, and a thumbnail of a large image must be embedded in each link. Ie, we will address the questions of how to automatically create a list of files in a folder and how to quickly add a list of files to the html code of the page.
Images laid out in the daddy on the local disk, but there are many of them and too lazy to copy-paste, and how much can you? Suddenly I discovered that my fingers are already dialing in the comstar:
dir /b /A:-D> spisok.txt
')
Dir will create a list of files,
/ b will leave only the names of files and their extensions,
/ A: -D will output only files, but not folders,
> will drive it all into a text file called
spisok.txt .
Now the resulting file is opened in Notepad ++ (although you can probably also in an editor with the same functionality), where you do the following:
Ctrl + H , a window opens with the
Replace tab,
Find What:(.*)\.(.*)
Replace with:
At the bottom, put a tick
Regular Expression , and
Wrap around and
. matches newlin remove.
Click
Replace All .
However, if the file name contains more than one current, then problems will arise. See the end of the article and comments.The command written to
Find does the following:
selects in the found line everything up to a point, then it itself and everything after it. Moreover, everything up to the point is stored at number 1, and everything after it, at number 2. We will use this later.
Further, the command replaces the selected string with what is written in
Replace with , i.e. to the link tag in which the path is registered and in which the tag of the picture with the thumbnail of the large image is attached.
Now, consider each line.
(.*)\.(.*)
. - means to find any character
* - means take as much as possible
those. "
. * " Would mean something like the following: find and select as many characters as possible, any. Those. select the whole line. And this command will highlight everything if it were not for:
\. - means to select a point.
However, the point has just been described by me as a command highlighting any character. The fact is that
Regex uses the "
\ " symbol as a command. This command means one of two things:
- if some special character follows this symbol, which itself is used as a command, then output this character by itself, not the command. Those. "
. "He will perceive as" search for any character ", and"
\. "Just like a point. If you just type Backslesh "
\ ", then he will expect that this is a command, and double backslashes "
\\ " will just output the "
\ " symbol itself.
- if there is a number behind this symbol, then copy here what was in the bracket in the search (instead of “
\ 1 ”, what was placed in the first left bracket during the search, i.e. the file name to the full stop, and instead of “
\ 2 ", then the second, ie the file extension, etc.) - we will use this a little later.
In other words, if you write this:
(.*)\.(.*)
like this:
.*\..*
,
then the search result will not change, but Notepad ++ doesn’t mark for itself and for us what it found to the point and after it. Brackets are like markers that divide a string, in this case into two parts.

<img src="putj
<img src="putj
is again just a piece of text to end the <a> tag and start the tag, after which what was placed in brackets during the search is inserted again:
\\\ 1_small \. \ 2 - where “ \\ ” outputs the backslash that is needed in the file address, “ \ 1 ” displays the contents of the first bracket, “ \. "Displays a point, and \ 2 is the content of the second bracket.
_small is just a text, it is inserted before the full stop . You need to rename the thumbnails so that at the end of their name there is a _small suffix. It was in order to insert this _small before the extension, I made two brackets in the search, so that _small can enclose the file name on the left and the extension of the same file on the right.
"alt =" is again a piece of the tag, an alt attribute, and after it:
= "\ 1"> - where
\ 1 inserts the name of the file into the
alt attribute, and after it the closing tag is added.
Since the file extension from its name is always separated only by a dot, I do not order “memorizing” it (ie, I don’t put brackets in the search), but, in fact, I could write it this way:
Find What: (.*)(\.)(.*)
Replace With: 
The result will be the same. But this expression is more flexible, because you can enter in the second bracket, for example:
[[: s:]] - select a space. The search command will look like this:
(.*)([[:s:]])(.*)
[[: unicode:]] - selects a character whose Unicode code is greater than 255. The search command will look like this:
(.*)([[:unicode:]])(.*)
[[: punct:]] - selects one of the following characters:,
"'?!;: $% & () * + - / <> = @ [] \ ^ _ {} | ~ The search command will look like this:
(.*)([[:punct:]])(.*)
So you can select the beginning and end of a code or word, where the corresponding symbol is located in the middle. But this is beyond the scope of this article.
Thank you for your attention, I hope it was useful for someone to find out what I have stated here.
Resources:
List of regex commands with examples.Dir command descriptionUPD
If the file name contains more than one current, then problems will arise. The simplest way to avoid such complexity will be to rename files so that there are no points anywhere except extensions (a graphical editor with the mass file renaming function will do).
A more advanced version was proposed during the discussion of this article:
^ (. *) \. ([^ \.] +) $ © FilimoniC
Selects everything to the last dot in the file name. Those. to the one before extension.
An even more complex example is in the discussion from the user RumataEstora