When regular expressions google, I only come across huge articles and thick books. And I just need to understand how this works:

location ~* ^.+\.\w+$ { root /home/box/web/public; } 

Okay. I began to read the book, finished three chapters already, but I fully understand what is written above is still not there and, apparently, if you go this way, you will not be soon.

Could you just briefly explain the symbolism of this example.

  • The syntax of regular expressions is quite different (for example, in different specifications, the most common of which are: bre, ere, pcre). Initial information can be found even from a wikipedia article . - aleksandr barakin

1 answer 1

^ - means that the match will be searched from the beginning of the line, and not from any character. The expression /box/ will match both box-web and home-box-web, and /^box/ only to the first line.

$ - means binding to the end of the line /index\.php/ will match both site.ru/index.php and site.ru/index.php?v=3, the expression /index\.php$/ will correspond only to the first variant.

.+ - any number of characters. Point - any character, combination .+ - one or more any characters.

\. - Since the dot is a special character, in order to mark a dot, it must be escaped with a slash.

\w - any character that can make up the word \w+ - any number of such characters (one or more).

Those. The expression ^.+\.\w+$ covers almost any URL. The sight was originally on expressions of the form hello / index.php.

Regular expressions are special declarative languages. Learning it is not as easy as it may seem at first glance. Its declarative nature does not allow to use the experience and knowledge acquired, say, in imperative programming languages. Therefore, studying it should be patient - this can take time. Quantifiers and special symbols will have to be memorized and to achieve a complete understanding of their work as branching operators and cycles in imperative languages. In exchange, you get a very compact tool for string manipulation.