正则表达式贪婪与非贪婪模式

如:String str="abcaxc";

    Patter p="ab*c";

  贪婪匹配:正则表达式一般趋向于最大长度匹配,也就是所谓的贪婪匹配。如上面使用模式p匹配字符串str,结果就是匹配到:abcaxc(ab*c)。

  非贪婪匹配:就是匹配到结果就好,就少的匹配字符。如上面使用模式p匹配字符串str,结果就是匹配到:abc(ab*c)。
编程中如何区分两种模式

  默认是贪婪模式;在量词后面直接加上一个问号?就是非贪婪模式。

  量词:{m,n}:m到n个

     *:任意多个

     +:一个到多个

     ?:0或一个
server {
        listen       80;
        server_name  espressos.cn;
         index index.php index.htm index.html;
        root /app/www/default;
        location ~ .*.(sh|bash)?$
        {
                return 403;
        }
        location ~ .*.(php|php5)?$#非贪婪模式
        {
                #fastcgi_pass  unix:/tmp/php-cgi.sock;
                fastcgi_pass  127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi.conf;
        }
        location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$#贪婪模式
        {
                expires 30d;
        }
        location ~ .*.(js|css)?$#非贪婪模式
        {
                expires 1h;
        }
        include /app/server/nginx/conf/rewrite/default.conf;
        access_log  /app/log/nginx/access/default.log;
}
原文地址:https://www.cnblogs.com/bass6/p/5725263.html