玩轉 Nginx 之:使用 Lua 擴展 Nginx 功能

1、Nginx 簡介

Nginx 作為一款面向性能設計的HTTP服務器,相較於Apache、lighttpd具有佔有內存少,穩定性高等優勢。其流行度越來越高,應用也越來越廣泛,常見的應用有:網頁服務器、反向代理服務器以及電子郵件(IMAP/POP3)代理服務器,高併發大流量站點常用來做接入層的負載均衡,還有非常常見的用法是作為日誌採集服務器等。

Nginx 整體採用模塊化設計,有豐富的模塊庫和第三方模塊庫,配置靈活。其中模塊化設計是nginx的一大賣點,甚至http服務器核心功能也是一個模塊。要注意的是:nginx的模塊是靜態的,添加和刪除模塊都要對nginx進行重新編譯,這一點與Apache的動態模塊完全不同。不過後來淘寶做了二次開發開源的 tengine 是支持 官方所有的 HTTP 模塊動態加載而不必重新編譯 Nginx,除非是第三方模塊才需要重新編譯。因此,在生產環境中,推薦用淘寶開源的 tengine,本文也以 tengine 作為示例。

雖然 Nginx 有如此強大的性能以及眾多的三方模塊支持,但每次重新編譯以及尋找三方模塊對生產環境來說還是不可接受的,幸運的是,Nginx 它是支持客戶自己 Lua 腳本編程擴展相應的功能的,而且可以熱加載,這就給生產環境帶來了無限可能。比如我現在想要直接用Nginx + redis 做反爬蟲和頻率限制,Nginx + Kafka 做日誌的實時流處理等等。

注:lvs 和 nginx 的負載均衡區別:

LVS:Linux Virtual Server,基於IP的負載均衡和反向代理技術,所以它幾乎可以對所有應用做負載均衡,包括http、數據庫、在線聊天室等等,LVS工作在4層,在Linux內核中作四層交換,只花128個字節記錄一個連接信息,不涉及到文件句柄操作,故沒有65535最大文件句柄數的限制。LVS性能很高,可以支持100~400萬條併發連接。抗負載能力強、是工作在網絡4層之上僅作分發之用,沒有流量的產生,這個特點也決定了它在負載均衡軟件裡的性能最強的,對內存和cpu、IO資源消耗比較低。

Nginx:基於HTTP的負載均衡和反向代理服務器,Nginx工作在網絡的7層,所以它可以針對http應用本身來做分流策略,比如針對域名、URL、目錄結構等,相比之下LVS並不具備這樣的功能,能夠很好地支持虛擬主機,可配置性很強,大約能支持3~5萬條併發連接。

2、Lua 簡介

Lua 是一個簡潔、輕量、可擴展的腳本語言,也是號稱性能最高的腳本語言,用在很多需要性能的地方,比如:遊戲腳本,nginx,wireshark的腳本,當你把他的源碼下下來編譯後,你會發現解釋器居然不到200k,非常變態。。。很多應用程序使用Lua作為自己的嵌入式腳本語言,以此來實現可配置性、可擴展性。

Lua原生支持的數據類型非常之少,它只提供了nil、數字(缺省是雙精度浮點數,可配置)、布爾量、字符串、表、子程序、協程(coroutine)以及用戶自定義數據這8種。但是其處理表和字符串的效率非常之高,加上元表的支持,開發者可以高效的模擬出需要的複雜數據類型(比如集合、數組等)。Lua是一個動態弱類型語言,支持增量式垃圾收集策略。有內建的,與操作系統無關的協作式多線程(coroutine)支持。它還可以用於嵌入式硬件,不僅可以嵌入其他編程語言,而且可以嵌入微處理器中。

3、nginx執行步驟

nginx在處理每一個用戶請求時,都是按照若干個不同的階段依次處理的,與配置文件上的順序沒有關係,詳細內容可以閱讀《深入理解nginx:模塊開發與架構解析》這本書,這裡只做簡單介紹;

(1)post-read

讀取請求內容階段,nginx讀取並解析完請求頭之後就立即開始運行;

(2)server-rewrite

server請求地址重寫階段;

(3)find-config

配置查找階段,用來完成當前請求與location配重塊之間的配對工作;

(4)rewrite

location請求地址重寫階段,當ngx_rewrite指令用於location中,就是再這個階段運行的;

(5)post-rewrite

請求地址重寫提交階段,當nginx完成rewrite階段所要求的內部跳轉動作,如果rewrite階段有這個要求的話;

(6)preaccess

訪問權限檢查準備階段,ngx_limit_req和ngx_limit_zone在這個階段運行,ngx_limit_req可以控制請求的訪問頻率,ngx_limit_zone可以控制訪問的併發度;

(7)access

權限檢查階段,ngx_access在這個階段運行,配置指令多是執行訪問控制相關的任務,如檢查用戶的訪問權限,檢查用戶的來源IP是否合法;

(8)post-access

訪問權限檢查提交階段;

(9)try-files

配置項try_files處理階段;

(10)content

內容產生階段,是所有請求處理階段中最為重要的階段,因為這個階段的指令通常是用來生成HTTP響應內容的;

(11)log

日誌模塊處理階段;

玩轉 Nginx 之:使用 Lua 擴展 Nginx 功能

圖:Nginx 模塊執行順序與階段

4、ngx_lua 運行指令

ngx_lua屬於nginx的一部分,它的執行指令都包含在nginx的11個步驟之中了,相應的處理階段可以做插入式處理,即可插拔式架構,不過ngx_lua並不是所有階段都會運行的;另外指令可以在http、server、server if、location、location if幾個範圍進行配置:

指令

所處處理階段

使用範圍

解釋

init_by_lua

init_by_lua_file

loading-config

http

nginx Master進程加載配置時執行;

通常用於初始化全局配置/預加載Lua模塊

init_worker_by_lua

init_worker_by_lua_file

starting-worker

http

每個Nginx Worker進程啟動時調用的計時器,如果Master進程不允許則只會在init_by_lua之後調用;

通常用於定時拉取配置/數據,或者後端服務的健康檢查

set_by_lua

set_by_lua_file

rewrite

server,server if,location,location if

設置nginx變量,可以實現複雜的賦值邏輯;此處是阻塞的,Lua代碼要做到非常快;

rewrite_by_lua

rewrite_by_lua_file

rewrite tail

http,server,location,location if

rrewrite階段處理,可以實現複雜的轉發/重定向邏輯;

access_by_lua

access_by_lua_file

access tail

http,server,location,location if

請求訪問階段處理,用於訪問控制

content_by_lua

content_by_lua_file

content

location,location if

內容處理器,接收請求處理並輸出響應

header_filter_by_lua

header_filter_by_lua_file

output-header-filter

http,server,location,location if

設置header和cookie

body_filter_by_lua

body_filter_by_lua_file

output-body-filter

http,server,location,location if

對響應數據進行過濾,比如截斷、替換。

log_by_lua

log_by_lua_file

log

http,server,location,location if

log階段處理,比如記錄訪問量/統計平均響應時間

關於這部分詳細可以參考這篇:

Refer

Refer

5、安裝 tengine 以及 Lua 擴展

(1)先安裝Nginx需要的一些類庫:

yum install gcc

yum install gcc-c++

注:此步驟只是在你的系統沒有安裝 gcc/gcc-c++ 的情況下才需要自行編譯安裝。

(2)編譯安裝庫LuaJit-2.0.3:

./configure --prefix=/usr/local/luajit

make PREFIX=/usr/local/luajit

make install PREFIX=/usr/local/luajit

在/etc/profile文件中增加環境變量,並執行 source /etc/profile 使之生效(非必須):

export LUAJIT_LIB=/usr/install/luajit/lib

export LUAJIT_INC=/usr/install/luajit/include/luajit-2.0

注:此步驟只是在你的系統沒有安裝 LuaJIT 的情況下才需要自行編譯安裝。

(3)下載模塊依賴 pcre-8.34、zlib-1.2.8、ngx_devel_kit 和 lua-nginx-module,最後編譯Nginx:

完整的參數可能這樣:

nginx -V
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx/ --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-cc-opt='-O2 -g -m64 -mtune=generic' --with-pcre=../pcre-8.33 --with-zlib=../zlib-1.2.8 --with-openssl=../openssl-1.0.1l

先 ./configure 各種配置參數以及模塊路徑,但我這裡只是用來測試就精簡了不少參數:

./configure --prefix=/opt/soft/nginx --with-pcre=/root/soft/pcre-8.37 --with-zlib=/root/soft/zlib-1.2.8 --with-openssl=/root/soft/openssl-1.0.1p --add-module=/root/soft/lua-nginx-module-master --add-module=/root/soft/ngx_devel_kit-master --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp

然後 make、make install 即可。

(5)啟動Nginx sbin/nginx,瀏覽器輸入http://localhost測試

需要注意的是:

(1)--with-pcre=/root/soft/pcre-8.37 --with-zlib=/root/soft/zlib-1.2.8 指向的是源碼路徑,而非編譯後的路徑,否則 make 會報:

cd /usr/local/pcre \
&& if [ -f Makefile ]; then make distclean; fi \
&& CC="gcc" CFLAGS="-O2 -fomit-frame-pointer -pipe " \
./configure --disable-shared
/bin/sh: ./configure: No such file or directory
make[1]: *** [/usr/local/pcre/Makefile] Error 127
make[1]: Leaving directory `/software/nginx-0.8.53'
make: *** [build] Error 2

因為 ./configure --help 看一下幫助說明:

--with-pcre=DIR set path to PCRE library sources

set path to PCRE library sources 是讓你設置到源碼目錄,而不是編譯安裝後的目錄

(2)pcre、zlib、openssl 等系統庫如果系統沒有集成自帶,一定要單獨編譯安裝,而 lua-nginx-module、ngx_devel_kit 等插件模塊只需要指源碼路徑給 nginx 一起編譯即可,不需要單獨編譯。

6、一個 Nginx + Lua 測試的例子

user work work;worker_processes 7;error_log /opt/data1/logs/nginx/error.log;pid /var/run/nginx.pid;worker_rlimit_nofile 800000;events { use epoll; worker_connections 65535;
}http {
server { listen 80; set $idTest "idTest_11111111111" ; log_format tracklog '$idTest $msec $remote_addr - $remote_user [$time_local] $request '
'"$status" resp_body:"$resp_body" --"$ref1"-- '

'"$http_user_agent"';
location ~ /gzip/(.*) { default_type "text/html";
set $resp_body ""; content_by_lua '
--zlib 解碼 post gzip 數據
local zlib = require "zlib"
local encoding = ngx.req.get_headers()["Content-Encoding"]

if encoding == "gzip" then
ngx.req.read_body()
local body = ngx.req.get_body_data()
ngx.say("++++++++++++++++++++++++++++body data:")
ngx.print(body)
if body then
--ngx.var.resp_body = "55555555555555"
local stream = zlib.inflate()
ngx.var.resp_body = stream(body)
end
end
';
access_log on; access_log /opt/data1/logs/nginx/pc/track/ooxx.com.access.log tracklog;
} location ~ /post/(.*) { default_type "text/html"; lua_need_request_body on;
set $resp_body ""; content_by_lua '
ngx.var.resp_body = ngx.var.request_body
';
access_log on; access_log /opt/data1/logs/nginx/pc/track/ooxx.com.access.log tracklog;
}

location ~ /lua/(.*) { default_type "text/html";
set $ref1 "Hello,Nginx & Lua !";
#設置nginx變量
set $a $1;
set $b $host;
content_by_lua '
--nginx變量
local var = ngx.var
ngx.say("ngx.var.a : ", var.a, "
")
ngx.say("ngx.var.b : ", var.b, "
")
ngx.say("ngx.var[2] : ", var[2], "
")
ngx.var.b = 2;
ngx.say("
")

--請求頭
ngx.say(ngx.var.httpRef, "
")
local headers = ngx.req.get_headers()

for k,v in pairs(headers) do
if type(v) == "table" then
ngx.say(k, " : ", table.concat(v, ","), "
")
else
ngx.say(k, " : ", v, "
")
end
end
ngx.say("------------headers end-----------", "


")

--get請求uri參數
ngx.say("uri args begin", "
")
local uri_args = ngx.req.get_uri_args()
for k, v in pairs(uri_args) do
if type(v) == "table" then
ngx.say(k, " : ", table.concat(v, ", "), "
")
else
ngx.say(k, ": ", v, "
")
end
end
ngx.say("uri args end", "
")
ngx.say("a: ",ngx.var.arg_a, "
")
ngx.say("b: ",ngx.var.arg_b, "
")

--未經解碼的請求 uri
local request_uri = headers["Host"] .. "/" .. ngx.var.request_uri;
ngx.say("request_uri : ", request_uri, "
");
--解碼後的 uri
local decode_request_uri = headers["Host"] .. "/" .. ngx.unescape_uri(ngx.var.request_uri);
ngx.var.ref1 = decode_request_uri;
ngx.say("decode request_uri : ", decode_request_uri, "
");
--MD5
ngx.say("ngx.md5 : ", ngx.md5("123"), "
")
--http time
ngx.say("ngx.http_time : ", ngx.http_time(ngx.time()), "
")

--ngx.var.http_referer = "*********************"

';


log_format LogFormatv1 '$idTest@$msec@$remote_addr@-@$remote_user@[$time_local]@$request@'
'"$status"@$body_bytes_sent@"$http_referer"@'
'"$http_user_agent"';
access_log on; access_log /opt/data1/logs/nginx/PCv1/track/ooxx.com.access.log LogFormatv1;
}

}
}

玩轉 Nginx 之:使用 Lua 擴展 Nginx 功能

需要注意的是線上的 Nginx 可能面對的是高併發場景,對於自己的 Lua 代碼最好做個壓力測試,比如:

tcpcopy 

或者

ab -c100 -n10000 'http://test.abc.com/lua/test%20haha/?a=3&b=4'

7、關於 64bit Cgywin 下編譯安裝 Tengine 的一些問題

(1)openSSL 庫不支持 64bit Cygwin

cryptlib.c:1:0: 錯誤:您選擇的 CPU 不支持 x86-64 指令集
/* crypto/cryptlib.c */ ^
cryptlib.c:1:0: 錯誤:您選擇的 CPU 不支持 x86-64 指令集
: recipe for target 'cryptlib.o' failed
make[3]: *** [cryptlib.o] Error 1make[3]: Leaving directory '/home/Jun/softs/openssl-1.0.1r/crypto'Makefile:281: recipe for target 'build_crypto' failed

這種要麼自己去網上找補丁 patch,要麼換成 Cygwin 32bit,還有一種解決方案就是自己在安裝 Cygwin 包的時候把 openssl 也裝上,也就是說選擇安裝包的時候儘量裝全一點。省得後續自己裝遇到各種問題。

這樣如果你自己已經裝了 openssl 那麼可以在configure 的時候去掉openssl 相關的編譯依賴選項:

./configure --with-openssl=/root/soft/openssl-1.0.1p (去掉該項)

(2)nginx 啟動報錯:

nginx: [emerg] the maximum number of files supported by select() is 64

畢竟 Cygwin 還是依賴 windows的,各種參數得針對 windows 優化,比如這個報錯是說默認配置文件裡的 worker_connections 不能超過 64 個,你改成 20 即可。

events {
use epoll;
worker_connections 65535;
}

比如我這裡的編譯配置參數是(注意有些目錄可能需要提前自己創建):

./configure --prefix=/opt/soft/nginx --with-pcre=/home/Jun/softs/pcre-8.38 --with-zlib=/home/Jun/softs/zlib-1.2.8 --add-module=/home/Jun/softs/ngx_devel_kit-master --add-module=/home/Jun/softs/echo-nginx-module-master --add-module=/home/Jun/softs/form-input-nginx-module-master --add-module=/home/Jun/softs/set-misc-nginx-module-master --add-module=/home/Jun/softs/lua-nginx-module-master --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp

最後 nginx.conf 稍加修改如下(注意有些目錄可能需要提前自己創建):

worker_processes 2;
#error_log /var/log/nginx/error.log;pid /var/run/nginx.pid;
events {
worker_connections 20;
}
http {

server {
listen 80;

set $idTest "idTest_11111111111" ;

log_format tracklog '$idTest $msec $remote_addr - $remote_user [$time_local] $request ' '"$status" $body_bytes_sent --"$ref1"-- ' '"$http_user_agent"';

log_format LogFormatv1 '$idTest@$msec@$remote_addr@-@$remote_user@[$time_local]@$request@' '"$status"@$body_bytes_sent@"$http_referer"@' '"$http_user_agent"';

access_log on;

location ~ /lua/(.*) {
default_type "text/html";
set $ref1 "Hello,Nginx & Lua !";
#設置nginx變量 set $a $1;
set $b $host;

content_by_lua '
--nginx變量

local var = ngx.var
ngx.say("ngx.var.a : ", var.a, "
")
ngx.say("ngx.var.b : ", var.b, "
")
ngx.say("ngx.var[2] : ", var[2], "
")
ngx.var.b = 2;
ngx.say("
")

--請求頭
ngx.say(ngx.var.httpRef, "
")
local headers = ngx.req.get_headers()
for k,v in pairs(headers) do
if type(v) == "table" then
ngx.say(k, " : ", table.concat(v, ","), "
")
else
ngx.say(k, " : ", v, "
")
end
end
ngx.say("------------headers end-----------", "


")

--get請求uri參數
ngx.say("uri args begin", "
")
local uri_args = ngx.req.get_uri_args()
for k, v in pairs(uri_args) do
if type(v) == "table" then
ngx.say(k, " : ", table.concat(v, ", "), "
")
else
ngx.say(k, ": ", v, "
")
end
end
ngx.say("uri args end", "
")
ngx.say("a: ",ngx.var.arg_a, "
")
ngx.say("b: ",ngx.var.arg_b, "
")

--未經解碼的請求 uri

local request_uri = headers["Host"] .. "/" .. ngx.var.request_uri;
ngx.say("request_uri : ", request_uri, "
");
--解碼後的 uri
local decode_request_uri = headers["Host"] .. "/" .. ngx.unescape_uri(ngx.var.request_uri);
ngx.var.ref1 = decode_request_uri;
ngx.say("decode request_uri : ", decode_request_uri, "
");
--MD5
ngx.say("ngx.md5 : ", ngx.md5("123"), "
")
--http time
ngx.say("ngx.http_time : ", ngx.http_time(ngx.time()), "
")
--ngx.var.http_referer = "*********************"
';

access_log /var/log/nginx/ooxx.com.access.log tracklog;
}

}
}

最後 nginx -s reload 效果如下, 請求也正常,和 linux 下結果一致:

玩轉 Nginx 之:使用 Lua 擴展 Nginx 功能

8、關於 nginx 正則說明

(1)location 匹配語法規則

Nginx location 的正則匹配語法與優先級容易讓新同學迷惑。

~ #波浪線表示執行一個正則匹配,區分大小寫

~* #表示執行一個正則匹配,不區分大小寫

= #進行普通字符精確匹配,與location在配置文件中的順序無關,= 精確匹配會第一個被處理

@ #"@" 定義一個命名的 location,使用在內部定向時,例如 error_page, try_files

^~ 標識符後面跟一個字符串。表示普通字符匹配,如果該選項匹配,只匹配該選項,不匹配別的選項,Nginx將在這個字符串匹配後停止進行正則表達式的匹配(location指令中正則表達式的匹配的結果優先使用),如:location ^~ /images/,你希望對/images/這個目錄進行一些特別的操作,如增加expires頭,防盜鏈等,但是你又想把除了這個目錄的圖片外的所有圖片只進行增加expires頭的操作,這個操作可能會用到另外一個location,例如:location ~* \.(gif|jpg|jpeg)$,這樣,如果有請求/images/1.jpg,nginx如何決定去進行哪個location中的操作呢?結果取決於標識符^~,如果你這樣寫:location /images/,這樣nginx會將1.jpg匹配到location ~* \.(gif|jpg|jpeg)$這個location中,這並不是你需要的結果,而增加了^~這個標識符後,它在匹配了/images/這個字符串後就停止搜索其它帶正則的location。

例如:

location = / { # 只匹配"/". [ configuration A ] 
}
location / { # 匹配任何請求,因為所有請求都是以"/"開始 # 但是更長字符匹配或者正則表達式匹配會優先匹配 [ configuration B ]
}
location ^~ /images/ { # 匹配任何以 /images/ 開始的請求,並停止匹配 其它location [ configuration C ]
}
location ~* \.(gif|jpg|jpeg)$ { # 匹配以 gif, jpg, or jpeg結尾的請求. # 但是所有 /images/ 目錄的請求將由 [Configuration C]處理. [ configuration D ]
}

請求URI例子:

  • / -> 符合configuration A

  • /documents/document.html -> 符合configuration B

  • /images/1.gif -> 符合configuration C

  • /documents/1.jpg ->符合 configuration D

= 表示精確的查找地址,如location = /它只會匹配uri為/的請求,如果請求為/index.html,將查找另外的location,而不會匹配這個,當然可以寫兩個location,location = /和location /,這樣/index.html將匹配到後者,如果你的站點對/的請求量較大,可以使用這個方法來加快請求的響應速度。

@ 表示為一個location進行命名,即自定義一個location,這個location不能被外界所訪問,只能用於Nginx產生的子請求,主要為error_page和try_files。

(2)location 優先級官方文檔

  1. =前綴的指令嚴格匹配這個查詢。如果找到,停止搜索。

  2. 所有剩下的常規字符串,最長的匹配。如果這個匹配使用^〜前綴,搜索停止。

  3. 正則表達式,在配置文件中定義的順序。

  4. 如果第3條規則產生匹配的話,結果被使用。否則,如同從第2條規則被使用。

(3)正則語法

~ 為區分大小寫的匹配。

~* 不區分大小寫的匹配(匹配firefox的正則同時匹配FireFox)。

!~ 不匹配的

!~* 不匹配的

. 匹配除換行符以外的任意字符

\w 匹配字母或數字或下劃線或漢字

\s 匹配任意的空白符

\d 匹配數字

\b 匹配單詞的開始或結束

^ 匹配字符串的開始

$ 匹配字符串的結束

\W 匹配任意不是字母,數字,下劃線,漢字的字符

\S 匹配任意不是空白符的字符

\D 匹配任意非數字的字符

\B 匹配不是單詞開頭或結束的位置

捕獲 (exp) 匹配exp,並捕獲文本到自動命名的組裡

(?exp) 匹配exp,並捕獲文本到名稱為name的組裡,也可以寫成(?'name'exp)

(?:exp) 匹配exp,不捕獲匹配的文本,也不給此分組分配組號

零寬斷言 (?=exp) 匹配exp前面的位置

(?<=exp) 匹配exp後面的位置

(?!exp) 匹配後面跟的不是exp的位置

(?


分享到:


相關文章: